Here you can find the source of concatIterators(final Iterator
Parameter | Description |
---|---|
iterators | The iterators to concatenate. |
E | The type of the iterators. |
@SafeVarargs public static <E> Iterator<E> concatIterators(final Iterator<E>... iterators)
//package com.java2s; import java.util.*; public class Main { /**/*from w w w. j av a2 s .c o m*/ * Concatenate a number of iterators together, to form one big iterator. * This should respect the remove() functionality of the constituent iterators. * * @param iterators The iterators to concatenate. * @param <E> The type of the iterators. * @return An iterator consisting of all the component iterators concatenated together in order. */ @SafeVarargs public static <E> Iterator<E> concatIterators(final Iterator<E>... iterators) { return new Iterator<E>() { Iterator<E> lastIter = null; List<Iterator<E>> iters = new LinkedList<>(Arrays.asList(iterators)); @Override public boolean hasNext() { return !iters.isEmpty() && iters.get(0).hasNext(); } @Override public E next() { if (!hasNext()) { throw new IllegalArgumentException("Iterator is empty!"); } E next = iters.get(0).next(); lastIter = iters.get(0); while (!iters.isEmpty() && !iters.get(0).hasNext()) { iters.remove(0); } return next; } @Override public void remove() { if (lastIter == null) { throw new IllegalStateException("Call next() before calling remove()!"); } lastIter.remove(); } }; } public static List<Integer> asList(int[] a) { List<Integer> result = new ArrayList<>(a.length); for (int j : a) { result.add(Integer.valueOf(j)); } return result; } public static List<Double> asList(double[] a) { List<Double> result = new ArrayList<>(a.length); for (double v : a) { result.add(new Double(v)); } return result; } }