Here you can find the source of multiSetIterator(Iterator extends X>... iterable)
public static <X> Iterator<X> multiSetIterator(Iterator<? extends X>... iterable)
//package com.java2s; //License from project: Open Source License import java.util.Arrays; import java.util.HashSet; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Set; public class Main { public static <X> Iterator<X> multiSetIterator(Iterator<? extends X>... iterable) { return multiSetIterator(Arrays.asList(iterable)); }//from ww w.j a v a2 s . c om public static <X> Iterator<X> multiSetIterator(Iterator<? extends Iterator<? extends X>> iterable) { return new Iterator<X>() { Iterator<? extends X> curIter = null; Set<X> ret = new HashSet<>(); boolean haveNext = false; X next = null; @Override public boolean hasNext() { if (haveNext) { return true; } while (curIter.hasNext()) { if (ret.add(next = curIter.next())) { haveNext = true; return true; } } while (iterable.hasNext()) { curIter = iterable.next(); if (curIter == null) { continue; } while (curIter.hasNext()) { if (ret.add(next = curIter.next())) { haveNext = true; return true; } } } return false; } @Override public X next() { if (hasNext()) { haveNext = false; return next; } throw new NoSuchElementException(); } }; } public static <X> Iterator<X> multiSetIterator(Iterable<? extends Iterator<? extends X>> iterable) { return multiSetIterator(iterable.iterator()); } }