Java Collection Union union(Collection collection1, Collection collection2)

Here you can find the source of union(Collection collection1, Collection collection2)

Description

Given two collections of elements of type , return a collection containing the items from both lists

License

Open Source License

Parameter

Parameter Description
T Type
collection1 Collection #1
collection2 Collection #2

Return

Collection with items from both lists

Declaration

public static <T> Collection<T> union(Collection<? extends T> collection1,
        Collection<? extends T> collection2) 

Method Source Code

//package com.java2s;
/**//www. j a va  2s .  co m
 * Aptana Studio
 * Copyright (c) 2005-2011 by Appcelerator, Inc. All Rights Reserved.
 * Licensed under the terms of the GNU Public License (GPL) v3 (with exceptions).
 * Please see the license.html included with this distribution for details.
 * Any modifications to this file must keep this entire header intact.
 */

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

import java.util.HashSet;

import java.util.Map;
import java.util.Set;

public class Main {
    /**
     * Given two collections of elements of type <T>, return a collection containing the items from both lists
     * 
     * @param <T>
     *            Type
     * @param collection1
     *            Collection #1
     * @param collection2
     *            Collection #2
     * @return Collection with items from both lists
     */
    public static <T> Collection<T> union(Collection<? extends T> collection1,
            Collection<? extends T> collection2) {
        if (isEmpty(collection1)) {
            if (isEmpty(collection2)) {
                // if both are empty, return empty list
                return Collections.emptyList();
            }
            // if just 1 is empty, return 2
            return new ArrayList<T>(collection2);
        }
        // at this point when know 1 is not empty
        if (isEmpty(collection2)) {
            // so if 2 is, return 1.
            return new ArrayList<T>(collection1);
        }

        // we know both 1 and 2 aren't empty
        Set<T> union = new HashSet<T>(collection1.size() + collection2.size());

        union.addAll(collection1);
        union.addAll(collection2);

        return new ArrayList<T>(union);
    }

    /**
     * This is a convenience method that returns true if the specified collection is null or empty
     * 
     * @param <T>
     *            Any type of object
     * @param collection
     * @return
     */
    public static <T> boolean isEmpty(Collection<T> collection) {
        return collection == null || collection.isEmpty();
    }

    /**
     * This is a convenience method that returns true if the specified map is null or empty
     * 
     * @param <T>
     *            any type of key
     * @param <U>
     *            any type of value
     * @param map
     * @return
     */
    public static <T, U> boolean isEmpty(Map<T, U> map) {
        return map == null || map.isEmpty();
    }
}

Related

  1. union(Collection c1, Collection c2)
  2. union(Collection coll)
  3. union(Collection c1, Collection c2)
  4. union(Collection> sets)
  5. union(Collection a, Collection b)
  6. union(Collection c1, Collection c2)