Here you can find the source of shallowUnionColIter(Iterator extends Iterator extends X>> values)
public static <X> Iterable<X> shallowUnionColIter(Iterator<? extends Iterator<? extends X>> values)
//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> Iterable<X> shallowUnionColIter(Iterator<? extends Iterator<? extends X>> values) { return () -> multiIterator(values); }//from ww w. jav a2s .c o m public static <X> Iterator<X> multiIterator(Iterable<? extends Iterator<? extends X>> iterable) { return multiIterator(iterable.iterator()); } 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 ((this.curIter == null || !this.curIter.hasNext()) && iterable.hasNext()) { while (iterable.hasNext()) { this.curIter = iterable.next(); break; } } return this.curIter != null && this.curIter.hasNext(); } @Override public X next() { if (this.curIter == null) { throw new NoSuchElementException(); } return this.curIter.next(); } }; } public static <X> Iterator<X> multiIterator(Iterator<? extends X>... iterable) { return multiIterator(Arrays.asList(iterable)); } }