Java Iterator concatIterators(final Iterator... iterators)

Here you can find the source of concatIterators(final Iterator... iterators)

Description

Concatenate a number of iterators together, to form one big iterator.

License

Open Source License

Parameter

Parameter Description
iterators The iterators to concatenate.
E The type of the iterators.

Return

An iterator consisting of all the component iterators concatenated together in order.

Declaration

@SafeVarargs
public static <E> Iterator<E> concatIterators(final Iterator<E>... iterators) 

Method Source Code

//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;
    }
}

Related

  1. asStringOn(StringBuffer sb, Iterator iter, String separator)
  2. cast(Iterator p)
  3. closeIterator(Iterator iterator)
  4. compareRanges(final Iterator i, final Iterator j)
  5. concatinateElements(Iterator it)
  6. concatLines(Iterator lineIterator)
  7. contains(final Iterator iter, final E item)
  8. contains(Iterator it, T o)
  9. contains(Iterator iterator, Object element)