Java List Union shallowUnionCol(Collection> values)

Here you can find the source of shallowUnionCol(Collection> values)

Description

shallow Union Col

License

Open Source License

Declaration

public static <X> Iterable<X> shallowUnionCol(Collection<List<X>> values) 

Method Source Code


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

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;

public class Main {
    public static <X> Iterable<X> shallowUnionCol(Collection<List<X>> values) {
        return shallowUnion(new ArrayList<>(values).toArray(new List[] {}));
    }/*  w  w w.  j  a v  a2s  .  c o m*/

    public static <X> Iterable<X> shallowUnion(Iterable<? extends X>... iterables) {
        return () -> {
            Iterator<? extends X>[] iters = new Iterator[iterables.length];
            for (int i = 0; i < iters.length; ++i) {
                iters[i] = iterables[i].iterator();
            }
            return multiIterator(iters);
        };
    }

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

    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. union(final char[]... list)
  2. union(final List list1, final List list2)
  3. union(final List list1, final List list2)
  4. union(final List list1, final List list2)