Java Iterator multiIterator(Iterator... iterable)

Here you can find the source of multiIterator(Iterator... iterable)

Description

multi Iterator

License

Open Source License

Declaration

public static <X> Iterator<X> multiIterator(Iterator<? extends X>... iterable) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Arrays;

import java.util.Iterator;

import java.util.NoSuchElementException;

public class Main {
    public static <X> Iterator<X> multiIterator(Iterator<? extends X>... iterable) {
        return multiIterator(Arrays.asList(iterable));
    }//  w w w. j av a  2 s  . co  m

    public static <X> Iterator<X> multiIterator(Iterator<? extends Iterator<? extends X>> iterable) {
        return new Iterator<X>() {

            Iterator<? extends X> curIter = null;

            @Override
            public boolean hasNext() {
                while ((curIter == null || !curIter.hasNext()) && iterable.hasNext()) {
                    while (iterable.hasNext()) {
                        curIter = iterable.next();
                        break;
                    }

                }
                return curIter != null && curIter.hasNext();
            }

            @Override
            public X next() {
                if (curIter == null) {
                    throw new NoSuchElementException();
                }
                return curIter.next();
            }
        };
    }

    public static <X> Iterator<X> multiIterator(Iterable<? extends Iterator<? extends X>> iterable) {
        return multiIterator(iterable.iterator());
    }
}

Related

  1. listOfIterator(Iterator iterator)
  2. makeCollection(final Iterator i)
  3. max(final Iterator iter, final int max)
  4. mergeIterator( final Iterator iterator1, final Iterator iterator2)
  5. moveNext(Iterator iterator)
  6. multiply(String string, Iterator placeholders, Iterator replacements)
  7. multiply(String string, Iterator placeholders, Iterator replacements)
  8. multiSetIterator(Iterator... iterable)
  9. nextPermutation(Iterator> it)