Java examples for java.util:Set Operation
Returns the union of two Collection s as an unmodifiable set.
/*//from w w w .j av a 2s .co m * -------------------- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright 2008-2009 Sun Microsystems, Inc. All rights reserved. * * The contents of this file are subject to the terms of the Common Development * and Distribution License("CDDL") (the "License"). You may not use this file * except in compliance with the License. * * You can obtain a copy of the License at * http://opensource.org/licenses/cddl1.php * See the License for the specific language governing permissions and limitations * under the License. * * When distributing the Covered Code, include this CDDL Header Notice in each file * and include the License file at http://opensource.org/licenses/cddl1.php. * If applicable, add the following below this CDDL Header, with the fields * enclosed by brackets [] replaced by your own identifying information: * "Portions Copyrighted [year] [name of copyright owner]" * -------------------- */ import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; import java.util.SortedMap; import java.util.SortedSet; import java.util.TreeMap; import java.util.TreeSet; public class Main{ public static void main(String[] argv){ Collection c1 = java.util.Arrays.asList("asdf","book2s.com"); Collection c2 = java.util.Arrays.asList("asdf","book2s.com"); System.out.println(union(c1,c2)); } /** * Returns the union of two {@link Collection}s as an unmodifiable set. */ public static <T> Set<T> union(Collection<T> c1, Collection<T> c2) { Set<T> union = newSet(c1); union.addAll(c2); return Collections.<T> unmodifiableSet(union); } /** * Creates a set that can be modified from the {@link Collection} provided. */ public static <T> Set<T> newSet(Collection<T> c) { return new HashSet<T>(CollectionUtil.nullAsEmpty(c)); } /** * Creates a set that can be modified from the arguments. */ public static <T> Set<T> newSet(T... arr) { // default to empty.. Set<T> ret = new HashSet<T>(); if (arr != null && arr.length != 0) { // not empty populate the set.. for (T t : arr) { ret.add(t); } } return ret; } /** * Protects from <code>null</code> and returns a new instance of * {@link HashSet}. * * if the parameter <strong>c</strong> is <strong>null</strong>. * * @param c * collection to check * @param <T> * the type of the collection * @return if null new {@link HashSet} otherwise the parameter that was * passed in or */ public static <T> Collection<T> nullAsEmpty(Collection<T> c) { return c == null ? new HashSet<T>() : c; } /** * Protects from <code>null</code> and returns a new instance of * {@link HashMap} if the parameter <code>map</code> is <code>null</code>. * Otherwise return the parameter that was passed in. */ public static <T, K> Map<T, K> nullAsEmpty(Map<T, K> map) { return (map == null) ? new HashMap<T, K>() : map; } /** * Protects from <code>null</code> and returns a new instance of * {@link HashSet} if the parameter <code>set</code> is <code>null</code>. * Otherwise return the parameter that was passed in. */ public static <T> Set<T> nullAsEmpty(Set<T> set) { return (set == null) ? new HashSet<T>() : set; } /** * Protects from <code>null</code> and returns a new instance of * {@link ArrayList} if the parameter <code>list</code> is <code>null</code> * . Otherwise return the parameter that was passed in. */ public static <T> List<T> nullAsEmpty(final List<T> list) { return (list == null) ? new ArrayList<T>() : list; } }