Java Code Examples for java.util.ListIterator#forEachRemaining()

The following examples show how to use java.util.ListIterator#forEachRemaining() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: j2objc   File: ForEachRemainingTester.java    License: Apache License 2.0 5 votes vote down vote up
public static <T> void test_forEachRemaining_list(List<T> collection, T[] initialData)
        throws Exception {
    test_forEachRemaining(collection, initialData);

    ArrayList<T> recorder = new ArrayList<>();
    ListIterator<T> lit = collection.listIterator(1);

    lit.forEachRemaining((T i) -> recorder.add(i));
    if (initialData.length > 0) {
        assertEquals(initialData.length - 1, recorder.size());
        for (int i = 1; i < initialData.length; i++){
            assertEquals(initialData[i], recorder.get(i - 1));
        }
    }
}
 
Example 2
public void testOptimizedForEach() throws Exception {
    final Integer[] data = new Integer[1000 * 1000];
    for (int i=0; i < data.length; i++) {
        data[i] = i;
    }
    final List<Integer> source = Arrays.asList(data);

    final String[] listClasses = {
            "java.util.ArrayList",
            "java.util.LinkedList",
            "java.util.Vector",
            "java.util.concurrent.CopyOnWriteArrayList"
    };

    final int OFFSET = 3;
    final List<Integer> target = new ArrayList<>(source);
    for (final String listClass : listClasses) {
        final List<Integer> list =
                (List<Integer>) Class.forName(listClass).newInstance();
        list.addAll(source);
        final ListIterator<Integer> iterator = list.listIterator();
        assertFalse(iterator.hasPrevious());
        for (int i=0; i < OFFSET; i++) {
            iterator.next();
        }
        assertTrue(iterator.hasNext());
        assertTrue(iterator.hasPrevious());
        assertEquals(iterator.nextIndex(), OFFSET);
        assertEquals(iterator.previousIndex(), OFFSET - 1);

        iterator.forEachRemaining(e -> {
            target.set(e, e + 1);
        });
        for (int i=OFFSET; i < data.length; i++) {
            assertEquals(target.get(i).intValue(), source.get(i)+1);
        }

        assertFalse(iterator.hasNext());
        assertTrue(iterator.hasPrevious());
        assertEquals(iterator.nextIndex(), data.length);
        assertEquals(iterator.previousIndex(), data.length - 1);

        // CopyOnWriteArrayList.listIterator().remove() is unsupported
        if (!"java.util.concurrent.CopyOnWriteArrayList".equals(listClass)) {
            for (int i = data.length - 1; i >= 0; i--) {
                iterator.remove(); // must not throw
                if (i > 0) {
                    iterator.previous();
                }
            }
            assertTrue(list.isEmpty());
        }

        try {
            iterator.next();
            fail(listClass + " iterator advanced beyond end");
        } catch (NoSuchElementException ignore) {
        }
    }
}
 
Example 3
public void testOptimizedForEach() throws Exception {
    final Integer[] data = new Integer[1000 * 1000];
    for (int i=0; i < data.length; i++) {
        data[i] = i;
    }
    final List<Integer> source = Arrays.asList(data);

    final String[] listClasses = {
            "java.util.ArrayList",
            "java.util.LinkedList",
            "java.util.Vector",
            "java.util.concurrent.CopyOnWriteArrayList"
    };

    final int OFFSET = 3;
    final List<Integer> target = new ArrayList<>(source);
    for (final String listClass : listClasses) {
        final List<Integer> list =
                (List<Integer>) Class.forName(listClass).newInstance();
        list.addAll(source);
        final ListIterator<Integer> iterator = list.listIterator();
        assertFalse(iterator.hasPrevious());
        for (int i=0; i < OFFSET; i++) {
            iterator.next();
        }
        assertTrue(iterator.hasNext());
        assertTrue(iterator.hasPrevious());
        assertEquals(iterator.nextIndex(), OFFSET);
        assertEquals(iterator.previousIndex(), OFFSET - 1);

        iterator.forEachRemaining(e -> {
            target.set(e, e + 1);
        });
        for (int i=OFFSET; i < data.length; i++) {
            assertEquals(target.get(i).intValue(), source.get(i)+1);
        }

        assertFalse(iterator.hasNext());
        assertTrue(iterator.hasPrevious());
        assertEquals(iterator.nextIndex(), data.length);
        assertEquals(iterator.previousIndex(), data.length - 1);

        // CopyOnWriteArrayList.listIterator().remove() is unsupported
        if (!"java.util.concurrent.CopyOnWriteArrayList".equals(listClass)) {
            for (int i = data.length - 1; i >= 0; i--) {
                iterator.remove(); // must not throw
                if (i > 0) {
                    iterator.previous();
                }
            }
            assertTrue(list.isEmpty());
        }

        try {
            iterator.next();
            fail(listClass + " iterator advanced beyond end");
        } catch (NoSuchElementException ignore) {
        }
    }
}
 
Example 4
public void testOptimizedForEach() throws Exception {
    final Integer[] data = new Integer[1000 * 1000];
    for (int i=0; i < data.length; i++) {
        data[i] = i;
    }
    final List<Integer> source = Arrays.asList(data);

    final String[] listClasses = {
            "java.util.ArrayList",
            "java.util.LinkedList",
            "java.util.Vector",
            "java.util.concurrent.CopyOnWriteArrayList"
    };

    final int OFFSET = 3;
    final List<Integer> target = new ArrayList<>(source);
    for (final String listClass : listClasses) {
        final List<Integer> list =
                (List<Integer>) Class.forName(listClass).newInstance();
        list.addAll(source);
        final ListIterator<Integer> iterator = list.listIterator();
        assertFalse(iterator.hasPrevious());
        for (int i=0; i < OFFSET; i++) {
            iterator.next();
        }
        assertTrue(iterator.hasNext());
        assertTrue(iterator.hasPrevious());
        assertEquals(iterator.nextIndex(), OFFSET);
        assertEquals(iterator.previousIndex(), OFFSET - 1);

        iterator.forEachRemaining(e -> {
            target.set(e, e + 1);
        });
        for (int i=OFFSET; i < data.length; i++) {
            assertEquals(target.get(i).intValue(), source.get(i)+1);
        }

        assertFalse(iterator.hasNext());
        assertTrue(iterator.hasPrevious());
        assertEquals(iterator.nextIndex(), data.length);
        assertEquals(iterator.previousIndex(), data.length - 1);

        // CopyOnWriteArrayList.listIterator().remove() is unsupported
        if (!"java.util.concurrent.CopyOnWriteArrayList".equals(listClass)) {
            for (int i = data.length - 1; i >= 0; i--) {
                iterator.remove(); // must not throw
                if (i > 0) {
                    iterator.previous();
                }
            }
            assertTrue(list.isEmpty());
        }

        try {
            iterator.next();
            fail(listClass + " iterator advanced beyond end");
        } catch (NoSuchElementException ignore) {
        }
    }
}
 
Example 5
public void testOptimizedForEach() throws Exception {
    final Integer[] data = new Integer[1000 * 1000];
    for (int i=0; i < data.length; i++) {
        data[i] = i;
    }
    final List<Integer> source = Arrays.asList(data);

    final String[] listClasses = {
            "java.util.ArrayList",
            "java.util.LinkedList",
            "java.util.Vector",
            "java.util.concurrent.CopyOnWriteArrayList"
    };

    final int OFFSET = 3;
    final List<Integer> target = new ArrayList<>(source);
    for (final String listClass : listClasses) {
        final List<Integer> list =
                (List<Integer>) Class.forName(listClass).newInstance();
        list.addAll(source);
        final ListIterator<Integer> iterator = list.listIterator();
        assertFalse(iterator.hasPrevious());
        for (int i=0; i < OFFSET; i++) {
            iterator.next();
        }
        assertTrue(iterator.hasNext());
        assertTrue(iterator.hasPrevious());
        assertEquals(iterator.nextIndex(), OFFSET);
        assertEquals(iterator.previousIndex(), OFFSET - 1);

        iterator.forEachRemaining(e -> {
            target.set(e, e + 1);
        });
        for (int i=OFFSET; i < data.length; i++) {
            assertEquals(target.get(i).intValue(), source.get(i)+1);
        }

        assertFalse(iterator.hasNext());
        assertTrue(iterator.hasPrevious());
        assertEquals(iterator.nextIndex(), data.length);
        assertEquals(iterator.previousIndex(), data.length - 1);

        // CopyOnWriteArrayList.listIterator().remove() is unsupported
        if (!"java.util.concurrent.CopyOnWriteArrayList".equals(listClass)) {
            for (int i = data.length - 1; i >= 0; i--) {
                iterator.remove(); // must not throw
                if (i > 0) {
                    iterator.previous();
                }
            }
            assertTrue(list.isEmpty());
        }

        try {
            iterator.next();
            fail(listClass + " iterator advanced beyond end");
        } catch (NoSuchElementException ignore) {
        }
    }
}
 
Example 6
public void testOptimizedForEach() throws Exception {
    final Integer[] data = new Integer[1000 * 1000];
    for (int i=0; i < data.length; i++) {
        data[i] = i;
    }
    final List<Integer> source = Arrays.asList(data);

    final String[] listClasses = {
            "java.util.ArrayList",
            "java.util.LinkedList",
            "java.util.Vector",
            "java.util.concurrent.CopyOnWriteArrayList"
    };

    final int OFFSET = 3;
    final List<Integer> target = new ArrayList<>(source);
    for (final String listClass : listClasses) {
        final List<Integer> list =
                (List<Integer>) Class.forName(listClass).newInstance();
        list.addAll(source);
        final ListIterator<Integer> iterator = list.listIterator();
        assertFalse(iterator.hasPrevious());
        for (int i=0; i < OFFSET; i++) {
            iterator.next();
        }
        assertTrue(iterator.hasNext());
        assertTrue(iterator.hasPrevious());
        assertEquals(iterator.nextIndex(), OFFSET);
        assertEquals(iterator.previousIndex(), OFFSET - 1);

        iterator.forEachRemaining(e -> {
            target.set(e, e + 1);
        });
        for (int i=OFFSET; i < data.length; i++) {
            assertEquals(target.get(i).intValue(), source.get(i)+1);
        }

        assertFalse(iterator.hasNext());
        assertTrue(iterator.hasPrevious());
        assertEquals(iterator.nextIndex(), data.length);
        assertEquals(iterator.previousIndex(), data.length - 1);

        // CopyOnWriteArrayList.listIterator().remove() is unsupported
        if (!"java.util.concurrent.CopyOnWriteArrayList".equals(listClass)) {
            for (int i = data.length - 1; i >= 0; i--) {
                iterator.remove(); // must not throw
                if (i > 0) {
                    iterator.previous();
                }
            }
            assertTrue(list.isEmpty());
        }

        try {
            iterator.next();
            fail(listClass + " iterator advanced beyond end");
        } catch (NoSuchElementException ignore) {
        }
    }
}
 
Example 7
public void testOptimizedForEach() throws Exception {
    final Integer[] data = new Integer[1000 * 1000];
    for (int i=0; i < data.length; i++) {
        data[i] = i;
    }
    final List<Integer> source = Arrays.asList(data);

    final String[] listClasses = {
            "java.util.ArrayList",
            "java.util.LinkedList",
            "java.util.Vector",
            "java.util.concurrent.CopyOnWriteArrayList"
    };

    final int OFFSET = 3;
    final List<Integer> target = new ArrayList<>(source);
    for (final String listClass : listClasses) {
        final List<Integer> list =
                (List<Integer>) Class.forName(listClass).newInstance();
        list.addAll(source);
        final ListIterator<Integer> iterator = list.listIterator();
        assertFalse(iterator.hasPrevious());
        for (int i=0; i < OFFSET; i++) {
            iterator.next();
        }
        assertTrue(iterator.hasNext());
        assertTrue(iterator.hasPrevious());
        assertEquals(iterator.nextIndex(), OFFSET);
        assertEquals(iterator.previousIndex(), OFFSET - 1);

        iterator.forEachRemaining(e -> {
            target.set(e, e + 1);
        });
        for (int i=OFFSET; i < data.length; i++) {
            assertEquals(target.get(i).intValue(), source.get(i)+1);
        }

        assertFalse(iterator.hasNext());
        assertTrue(iterator.hasPrevious());
        assertEquals(iterator.nextIndex(), data.length);
        assertEquals(iterator.previousIndex(), data.length - 1);

        // CopyOnWriteArrayList.listIterator().remove() is unsupported
        if (!"java.util.concurrent.CopyOnWriteArrayList".equals(listClass)) {
            for (int i = data.length - 1; i >= 0; i--) {
                iterator.remove(); // must not throw
                if (i > 0) {
                    iterator.previous();
                }
            }
            assertTrue(list.isEmpty());
        }

        try {
            iterator.next();
            fail(listClass + " iterator advanced beyond end");
        } catch (NoSuchElementException ignore) {
        }
    }
}
 
Example 8
public void testOptimizedForEach() throws Exception {
    final Integer[] data = new Integer[1000 * 1000];
    for (int i=0; i < data.length; i++) {
        data[i] = i;
    }
    final List<Integer> source = Arrays.asList(data);

    final String[] listClasses = {
            "java.util.ArrayList",
            "java.util.LinkedList",
            "java.util.Vector",
            "java.util.concurrent.CopyOnWriteArrayList"
    };

    final int OFFSET = 3;
    final List<Integer> target = new ArrayList<>(source);
    for (final String listClass : listClasses) {
        final List<Integer> list =
                (List<Integer>) Class.forName(listClass).newInstance();
        list.addAll(source);
        final ListIterator<Integer> iterator = list.listIterator();
        assertFalse(iterator.hasPrevious());
        for (int i=0; i < OFFSET; i++) {
            iterator.next();
        }
        assertTrue(iterator.hasNext());
        assertTrue(iterator.hasPrevious());
        assertEquals(iterator.nextIndex(), OFFSET);
        assertEquals(iterator.previousIndex(), OFFSET - 1);

        iterator.forEachRemaining(e -> {
            target.set(e, e + 1);
        });
        for (int i=OFFSET; i < data.length; i++) {
            assertEquals(target.get(i).intValue(), source.get(i)+1);
        }

        assertFalse(iterator.hasNext());
        assertTrue(iterator.hasPrevious());
        assertEquals(iterator.nextIndex(), data.length);
        assertEquals(iterator.previousIndex(), data.length - 1);

        // CopyOnWriteArrayList.listIterator().remove() is unsupported
        if (!"java.util.concurrent.CopyOnWriteArrayList".equals(listClass)) {
            for (int i = data.length - 1; i >= 0; i--) {
                iterator.remove(); // must not throw
                if (i > 0) {
                    iterator.previous();
                }
            }
            assertTrue(list.isEmpty());
        }

        try {
            iterator.next();
            fail(listClass + " iterator advanced beyond end");
        } catch (NoSuchElementException ignore) {
        }
    }
}
 
Example 9
public void testOptimizedForEach() throws Exception {
    final Integer[] data = new Integer[1000 * 1000];
    for (int i=0; i < data.length; i++) {
        data[i] = i;
    }
    final List<Integer> source = Arrays.asList(data);

    final String[] listClasses = {
            "java.util.ArrayList",
            "java.util.LinkedList",
            "java.util.Vector",
            "java.util.concurrent.CopyOnWriteArrayList"
    };

    final int OFFSET = 3;
    final List<Integer> target = new ArrayList<>(source);
    for (final String listClass : listClasses) {
        final List<Integer> list =
                (List<Integer>) Class.forName(listClass).newInstance();
        list.addAll(source);
        final ListIterator<Integer> iterator = list.listIterator();
        assertFalse(iterator.hasPrevious());
        for (int i=0; i < OFFSET; i++) {
            iterator.next();
        }
        assertTrue(iterator.hasNext());
        assertTrue(iterator.hasPrevious());
        assertEquals(iterator.nextIndex(), OFFSET);
        assertEquals(iterator.previousIndex(), OFFSET - 1);

        iterator.forEachRemaining(e -> {
            target.set(e, e + 1);
        });
        for (int i=OFFSET; i < data.length; i++) {
            assertEquals(target.get(i).intValue(), source.get(i)+1);
        }

        assertFalse(iterator.hasNext());
        assertTrue(iterator.hasPrevious());
        assertEquals(iterator.nextIndex(), data.length);
        assertEquals(iterator.previousIndex(), data.length - 1);

        // CopyOnWriteArrayList.listIterator().remove() is unsupported
        if (!"java.util.concurrent.CopyOnWriteArrayList".equals(listClass)) {
            for (int i = data.length - 1; i >= 0; i--) {
                iterator.remove(); // must not throw
                if (i > 0) {
                    iterator.previous();
                }
            }
            assertTrue(list.isEmpty());
        }

        try {
            iterator.next();
            fail(listClass + " iterator advanced beyond end");
        } catch (NoSuchElementException ignore) {
        }
    }
}
 
Example 10
public void testOptimizedForEach() throws Exception {
    final Integer[] data = new Integer[1000 * 1000];
    for (int i=0; i < data.length; i++) {
        data[i] = i;
    }
    final List<Integer> source = Arrays.asList(data);

    final String[] listClasses = {
            "java.util.ArrayList",
            "java.util.LinkedList",
            "java.util.Vector",
            "java.util.concurrent.CopyOnWriteArrayList"
    };

    final int OFFSET = 3;
    final List<Integer> target = new ArrayList<>(source);
    for (final String listClass : listClasses) {
        final List<Integer> list =
                (List<Integer>) Class.forName(listClass).newInstance();
        list.addAll(source);
        final ListIterator<Integer> iterator = list.listIterator();
        assertFalse(iterator.hasPrevious());
        for (int i=0; i < OFFSET; i++) {
            iterator.next();
        }
        assertTrue(iterator.hasNext());
        assertTrue(iterator.hasPrevious());
        assertEquals(iterator.nextIndex(), OFFSET);
        assertEquals(iterator.previousIndex(), OFFSET - 1);

        iterator.forEachRemaining(e -> {
            target.set(e, e + 1);
        });
        for (int i=OFFSET; i < data.length; i++) {
            assertEquals(target.get(i).intValue(), source.get(i)+1);
        }

        assertFalse(iterator.hasNext());
        assertTrue(iterator.hasPrevious());
        assertEquals(iterator.nextIndex(), data.length);
        assertEquals(iterator.previousIndex(), data.length - 1);

        // CopyOnWriteArrayList.listIterator().remove() is unsupported
        if (!"java.util.concurrent.CopyOnWriteArrayList".equals(listClass)) {
            for (int i = data.length - 1; i >= 0; i--) {
                iterator.remove(); // must not throw
                if (i > 0) {
                    iterator.previous();
                }
            }
            assertTrue(list.isEmpty());
        }

        try {
            iterator.next();
            fail(listClass + " iterator advanced beyond end");
        } catch (NoSuchElementException ignore) {
        }
    }
}
 
Example 11
public void testOptimizedForEach() throws Exception {
    final Integer[] data = new Integer[1000 * 1000];
    for (int i=0; i < data.length; i++) {
        data[i] = i;
    }
    final List<Integer> source = Arrays.asList(data);

    final String[] listClasses = {
            "java.util.ArrayList",
            "java.util.LinkedList",
            "java.util.Vector",
            "java.util.concurrent.CopyOnWriteArrayList"
    };

    final int OFFSET = 3;
    final List<Integer> target = new ArrayList<>(source);
    for (final String listClass : listClasses) {
        final List<Integer> list =
                (List<Integer>) Class.forName(listClass).newInstance();
        list.addAll(source);
        final ListIterator<Integer> iterator = list.listIterator();
        assertFalse(iterator.hasPrevious());
        for (int i=0; i < OFFSET; i++) {
            iterator.next();
        }
        assertTrue(iterator.hasNext());
        assertTrue(iterator.hasPrevious());
        assertEquals(iterator.nextIndex(), OFFSET);
        assertEquals(iterator.previousIndex(), OFFSET - 1);

        iterator.forEachRemaining(e -> {
            target.set(e, e + 1);
        });
        for (int i=OFFSET; i < data.length; i++) {
            assertEquals(target.get(i).intValue(), source.get(i)+1);
        }

        assertFalse(iterator.hasNext());
        assertTrue(iterator.hasPrevious());
        assertEquals(iterator.nextIndex(), data.length);
        assertEquals(iterator.previousIndex(), data.length - 1);

        // CopyOnWriteArrayList.listIterator().remove() is unsupported
        if (!"java.util.concurrent.CopyOnWriteArrayList".equals(listClass)) {
            for (int i = data.length - 1; i >= 0; i--) {
                iterator.remove(); // must not throw
                if (i > 0) {
                    iterator.previous();
                }
            }
            assertTrue(list.isEmpty());
        }

        try {
            iterator.next();
            fail(listClass + " iterator advanced beyond end");
        } catch (NoSuchElementException ignore) {
        }
    }
}
 
Example 12
public void testOptimizedForEach() throws Exception {
    final Integer[] data = new Integer[1000 * 1000];
    for (int i=0; i < data.length; i++) {
        data[i] = i;
    }
    final List<Integer> source = Arrays.asList(data);

    final String[] listClasses = {
            "java.util.ArrayList",
            "java.util.LinkedList",
            "java.util.Vector",
            "java.util.concurrent.CopyOnWriteArrayList"
    };

    final int OFFSET = 3;
    final List<Integer> target = new ArrayList<>(source);
    for (final String listClass : listClasses) {
        final List<Integer> list =
                (List<Integer>) Class.forName(listClass).newInstance();
        list.addAll(source);
        final ListIterator<Integer> iterator = list.listIterator();
        assertFalse(iterator.hasPrevious());
        for (int i=0; i < OFFSET; i++) {
            iterator.next();
        }
        assertTrue(iterator.hasNext());
        assertTrue(iterator.hasPrevious());
        assertEquals(iterator.nextIndex(), OFFSET);
        assertEquals(iterator.previousIndex(), OFFSET - 1);

        iterator.forEachRemaining(e -> {
            target.set(e, e + 1);
        });
        for (int i=OFFSET; i < data.length; i++) {
            assertEquals(target.get(i).intValue(), source.get(i)+1);
        }

        assertFalse(iterator.hasNext());
        assertTrue(iterator.hasPrevious());
        assertEquals(iterator.nextIndex(), data.length);
        assertEquals(iterator.previousIndex(), data.length - 1);

        // CopyOnWriteArrayList.listIterator().remove() is unsupported
        if (!"java.util.concurrent.CopyOnWriteArrayList".equals(listClass)) {
            for (int i = data.length - 1; i >= 0; i--) {
                iterator.remove(); // must not throw
                if (i > 0) {
                    iterator.previous();
                }
            }
            assertTrue(list.isEmpty());
        }

        try {
            iterator.next();
            fail(listClass + " iterator advanced beyond end");
        } catch (NoSuchElementException ignore) {
        }
    }
}
 
Example 13
public void testOptimizedForEach() throws Exception {
    final Integer[] data = new Integer[1000 * 1000];
    for (int i=0; i < data.length; i++) {
        data[i] = i;
    }
    final List<Integer> source = Arrays.asList(data);

    final String[] listClasses = {
            "java.util.ArrayList",
            "java.util.LinkedList",
            "java.util.Vector",
            "java.util.concurrent.CopyOnWriteArrayList"
    };

    final int OFFSET = 3;
    final List<Integer> target = new ArrayList<>(source);
    for (final String listClass : listClasses) {
        final List<Integer> list =
                (List<Integer>) Class.forName(listClass).newInstance();
        list.addAll(source);
        final ListIterator<Integer> iterator = list.listIterator();
        assertFalse(iterator.hasPrevious());
        for (int i=0; i < OFFSET; i++) {
            iterator.next();
        }
        assertTrue(iterator.hasNext());
        assertTrue(iterator.hasPrevious());
        assertEquals(iterator.nextIndex(), OFFSET);
        assertEquals(iterator.previousIndex(), OFFSET - 1);

        iterator.forEachRemaining(e -> {
            target.set(e, e + 1);
        });
        for (int i=OFFSET; i < data.length; i++) {
            assertEquals(target.get(i).intValue(), source.get(i)+1);
        }

        assertFalse(iterator.hasNext());
        assertTrue(iterator.hasPrevious());
        assertEquals(iterator.nextIndex(), data.length);
        assertEquals(iterator.previousIndex(), data.length - 1);

        // CopyOnWriteArrayList.listIterator().remove() is unsupported
        if (!"java.util.concurrent.CopyOnWriteArrayList".equals(listClass)) {
            for (int i = data.length - 1; i >= 0; i--) {
                iterator.remove(); // must not throw
                if (i > 0) {
                    iterator.previous();
                }
            }
            assertTrue(list.isEmpty());
        }

        try {
            iterator.next();
            fail(listClass + " iterator advanced beyond end");
        } catch (NoSuchElementException ignore) {
        }
    }
}
 
Example 14
public void testOptimizedForEach() throws Exception {
    final Integer[] data = new Integer[1000 * 1000];
    for (int i=0; i < data.length; i++) {
        data[i] = i;
    }
    final List<Integer> source = Arrays.asList(data);

    final String[] listClasses = {
            "java.util.ArrayList",
            "java.util.LinkedList",
            "java.util.Vector",
            "java.util.concurrent.CopyOnWriteArrayList"
    };

    final int OFFSET = 3;
    final List<Integer> target = new ArrayList<>(source);
    for (final String listClass : listClasses) {
        final List<Integer> list =
                (List<Integer>) Class.forName(listClass).newInstance();
        list.addAll(source);
        final ListIterator<Integer> iterator = list.listIterator();
        assertFalse(iterator.hasPrevious());
        for (int i=0; i < OFFSET; i++) {
            iterator.next();
        }
        assertTrue(iterator.hasNext());
        assertTrue(iterator.hasPrevious());
        assertEquals(iterator.nextIndex(), OFFSET);
        assertEquals(iterator.previousIndex(), OFFSET - 1);

        iterator.forEachRemaining(e -> {
            target.set(e, e + 1);
        });
        for (int i=OFFSET; i < data.length; i++) {
            assertEquals(target.get(i).intValue(), source.get(i)+1);
        }

        assertFalse(iterator.hasNext());
        assertTrue(iterator.hasPrevious());
        assertEquals(iterator.nextIndex(), data.length);
        assertEquals(iterator.previousIndex(), data.length - 1);

        // CopyOnWriteArrayList.listIterator().remove() is unsupported
        if (!"java.util.concurrent.CopyOnWriteArrayList".equals(listClass)) {
            for (int i = data.length - 1; i >= 0; i--) {
                iterator.remove(); // must not throw
                if (i > 0) {
                    iterator.previous();
                }
            }
            assertTrue(list.isEmpty());
        }

        try {
            iterator.next();
            fail(listClass + " iterator advanced beyond end");
        } catch (NoSuchElementException ignore) {
        }
    }
}