Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//it under the terms of the GNU Affero General Public License as published by

import java.util.Collection;

import java.util.Enumeration;

import java.util.HashSet;
import java.util.Iterator;

import java.util.Set;

public class Main {
    /**
     * @param iterable iterates thru the collections to be merged into one.
     * @return a Set containing all the elements that are contained in at least one of the given collections.
     * Note that the result is a set so it will contain no duplicates (based on <code>E.equals()</code>).
     * @precondition iterable != null
     * @postcondition result != null
     */
    public static <E> Set<E> unionAll(Iterable<? extends Collection<? extends E>> iterable) {
        final Set<E> result = new HashSet<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;
    }
}