Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

public class Main {
    public static Collection union(final Collection c1, final Collection c2) {
        final List list = new ArrayList();
        addAllUnique(list, c1);
        addAllUnique(list, c2);
        return list;
    }

    /**
     * add a collection of values to a collection.
     * 
     * @param collection
     *            the collection.
     * @param values
     *            the values.
     * @return boolean.
     * @since 0.1
     */
    public static boolean addAllUnique(final Collection collection, final Collection values) {
        boolean changed = false;
        for (final Iterator iter = values.iterator(); iter.hasNext();) {
            changed = addUnique(collection, iter.next());
        }
        return changed;
    }

    /**
     * add a unique value to a collection. If the value already exists, it will
     * return <code>false</code>.
     * 
     * @param collection
     *            the collection.
     * @param value
     *            the value.
     * @return boolean.
     * @since 0.1
     */
    public static boolean addUnique(final Collection collection, final Object value) {
        if (collection.contains(value)) {
            return false;
        }
        return collection.add(value);
    }
}