Example usage for java.util ArrayDeque descendingIterator

List of usage examples for java.util ArrayDeque descendingIterator

Introduction

In this page you can find the example usage for java.util ArrayDeque descendingIterator.

Prototype

public Iterator<E> descendingIterator() 

Source Link

Usage

From source file:Main.java

public static void main(String[] args) {

    ArrayDeque<Integer> deque = new ArrayDeque<Integer>(8);

    deque.add(3);/*from  w  w  w.  j a va 2 s  . c o m*/
    deque.add(4);
    deque.add(25);
    deque.add(1);

    //iterator() is used to print all the elements
    //next() returns the next element on each iteration
    System.out.println("printing elements using iterator:");

    for (Iterator itr = deque.iterator(); itr.hasNext();) {
        System.out.println(itr.next());
    }

    //descendingIterator() is used to print the elements in reverse order
    //next() returns the next element on each iteration
    System.out.println("printing elements in reverse order:");

    for (Iterator descItr = deque.descendingIterator(); descItr.hasNext();) {
        System.out.println(descItr.next());
    }
}

From source file:com.google.gwt.emultest.java.util.ArrayDequeTest.java

public void testDescendingIterator() {
    Object o1 = new Object();
    Object o2 = new Object();
    Object o3 = new Object();

    ArrayDeque<Object> deque = new ArrayDeque<>();
    Iterator<Object> it = deque.descendingIterator();
    assertFalse(it.hasNext());//  w  w  w. j  a v  a  2  s . co  m
    try {
        it.next();
        fail();
    } catch (NoSuchElementException expected) {
    }

    deque.add(o1);
    deque.add(o2);
    deque.add(o3);
    it = deque.descendingIterator();
    assertTrue(it.hasNext());
    assertEquals(o3, it.next());
    assertTrue(it.hasNext());
    assertEquals(o2, it.next());
    assertTrue(it.hasNext());
    assertEquals(o1, it.next());
    assertFalse(it.hasNext());
    try {
        it.next();
        fail();
    } catch (NoSuchElementException expected) {
    }
    checkDequeSizeAndContent(deque, o1, o2, o3);

    deque = new ArrayDeque<>();
    deque.add(o1);
    deque.add(o2);
    deque.add(o3);
    it = deque.descendingIterator();
    assertTrue(it.hasNext());
    assertEquals(o3, it.next());
    it.remove();
    assertEquals(2, deque.size());
    assertTrue(it.hasNext());
    assertEquals(o2, it.next());
    assertTrue(it.hasNext());
    assertEquals(o1, it.next());
    it.remove();
    checkDequeSizeAndContent(deque, o2);
}

From source file:com.google.gwt.emultest.java.util.ArrayDequeTest.java

public void testFailFastDescendingIterator() {
    ArrayDeque<Object> deque = new ArrayDeque<>(asList(getFullNonNullElements()));
    Iterator<Object> it = deque.descendingIterator();
    it.next();/*w ww .  ja v  a 2  s.  c  o  m*/
    deque.removeLast();
    try {
        it.next();
    } catch (ConcurrentModificationException e) {
        fail();
    }
    deque.removeFirst();
    try {
        it.next();
        fail();
    } catch (ConcurrentModificationException expected) {
    }

    deque = new ArrayDeque<>(asList(getFullNonNullElements()));
    it = deque.descendingIterator();
    it.next();
    deque.clear();
    try {
        it.next();
        fail();
    } catch (ConcurrentModificationException expected) {
    }

    deque = new ArrayDeque<>(asList(getFullNonNullElements()));
    it = deque.descendingIterator();
    it.next();
    deque.addLast(new Object());
    try {
        it.next();
    } catch (ConcurrentModificationException e) {
        fail();
    }
    deque.addFirst(new Object());
    try {
        it.next();
        fail();
    } catch (ConcurrentModificationException expected) {
    }

    deque = new ArrayDeque<>(asList(getFullNonNullElements()));
    it = deque.descendingIterator();
    it.next();
    it.next();
    deque.removeLast();
    try {
        it.remove();
    } catch (ConcurrentModificationException e) {
        fail();
    }

    deque = new ArrayDeque<>(asList(getFullNonNullElements()));
    it = deque.descendingIterator();
    it.next();
    it.next();
    deque.removeLast();
    deque.removeLast();
    try {
        it.remove();
        fail();
    } catch (ConcurrentModificationException expected) {
    }
}