Java tutorial
//package com.java2s; //it under the terms of the GNU Affero General Public License as published by import java.util.ArrayList; import java.util.Collection; import java.util.Enumeration; import java.util.Iterator; import java.util.List; public class Main { /** * concatenates the given Collections to one Collection. * @param iterable iterates thru the collections to be merged into one. * @return the elements of all given Collections concatenated into one Collection. * @postcondition result != null */ public static <E> List<E> concatAll(Iterable<? extends Collection<? extends E>> iterable) { final List<E> result = new ArrayList<E>(); for (Collection<? extends E> coll : iterable) { result.addAll(coll); } assert result != null; return result; } /** * iterates over the given Iterator <code>iter</code> and adds all elements to the given Collection <code>collInOut</code>. * @param collInOut * @param iter * @precondition collInOut != null * @precondition iter != null * @return the same collection that has been passed as collInOut */ public static <E, C extends Collection<E>> C addAll(C collInOut, Iterator<? extends E> iter) { while (iter.hasNext()) { collInOut.add(iter.next()); } return collInOut; } /** * Iterates over the given Enumeration <code>e</code> and adds all elements to the given Collection <code>collInOut</code>. * @return the same collection that has been passed as collInOut */ public static <E, C extends Collection<E>> C addAll(C collInOut, Enumeration<? extends E> e) { while (e.hasMoreElements()) { collInOut.add(e.nextElement()); } return collInOut; } /** * same as addAll above, only using an array of elements * @param collInOut * @param elems * @precondition collInOut != null * @return the same collection that has been passed as collInOut */ public static <E, C extends Collection<E>> C addAll(C collInOut, E[] elems) { for (E e : elems) collInOut.add(e); return collInOut; } }