Example usage for java.util Set addAll

List of usage examples for java.util Set addAll

Introduction

In this page you can find the example usage for java.util Set addAll.

Prototype

boolean addAll(Collection<? extends E> c);

Source Link

Document

Adds all of the elements in the specified collection to this set if they're not already present (optional operation).

Usage

From source file:Main.java

public static <A> Set<A> makeSet(Collection<A> xs) {
    if (xs.size() == 0)
        return Collections.<A>emptySet();
    else if (xs.size() == 1)
        return Collections.singleton(xs.iterator().next());
    else {/*from w w w.  j  a  v  a  2 s  .co m*/
        Set<A> set = new HashSet<A>(xs.size());
        set.addAll(xs);
        return set;
    }
}

From source file:Main.java

public static Set<?> asSet(Object o) {
    if (o instanceof Set) {
        return (Set<?>) o;
    } else if (o instanceof Collection) {
        Set<Object> set = createDefaultSet();
        set.addAll((Collection<?>) o);
        return set;
    } else {/* www .  j  ava 2s .  c om*/
        Set<Object> set = createDefaultSet();
        set.add(o);
        return set;
    }
}

From source file:Main.java

public static <K> Set<K> createHashSet(K[] ks) {
    Set<K> kSet = new HashSet<K>(ks.length, 1);
    kSet.addAll(Arrays.asList(ks));
    return kSet;//from  www. j  a va 2  s .  c  o  m
}

From source file:Main.java

/**
 * Returns a {@link Set} containing the union of the given {@link Set}s.
 * //from   w w  w.j av a 2 s  . c o m
 * @param <T> type of items.
 * @param a the first set, must not be <code>null</code>
 * @param b the second set, must not be <code>null</code>
 * @return the union of the two.
 */
public static <T> Set<T> union(final Set<? extends T> a, final Set<? extends T> b) {
    final Set<T> res = new HashSet<T>(a);
    if (a != b)
        res.addAll(b);
    return res;
}

From source file:SetOps.java

/**
 * //from   ww w  . j a  v  a 2s.  co m
 * @param <T>
 * @param s1
 * @param s2
 * @return the union of s1 and s2. (The union of two sets is the set
 *         containing all of the elements contained in either set.)
 */
public static <T> Set<T> union(Set<T> s1, Set<T> s2) {
    Set<T> union = new LinkedHashSet<T>(s1);
    union.addAll(s2);
    return union;
}

From source file:Main.java

/**
 * Removes the duplicates by modifying the source list and keeping the order of the elements.
 *
 * @param <T>//  w  w  w.  j ava 2s.  c  o m
 *            the generic type
 * @param list
 *            the list
 * @return the same object as the argument
 */
public static <T> List<T> removeDuplicates(List<T> list) {
    Set<T> temp = createLinkedHashSet(list.size());
    temp.addAll(list);
    list.clear();
    list.addAll(temp);
    return list;
}

From source file:Main.java

/**
 * This method performs a union on two collections of elements as per definition of the <i>union</i> operation in set theory. 
 * The resulting collection guarantees unique membership as per implementation of the Java {@link java.util.Set} interface.
 * This is a null-safe operation./*from w  w w  .ja va 2  s  .c o  m*/
 * @see <a href="http://en.wikipedia.org/wiki/Union_(set_theory)">Union (set theory)</a>
 * @param firstGroup Collection to be used.
 * @param secondGroup Collection to be used.
 * @return The resulting collection containing all distinct members from the two groups.
 */
public static <T> Collection<T> union(Collection<T> firstGroup, Collection<T> secondGroup) {

    if (firstGroup == null)
        return secondGroup;
    else if (secondGroup == null)
        return firstGroup;
    else {
        Set<T> firstSet = new HashSet<T>(firstGroup);
        firstSet.addAll(secondGroup);
        return firstSet;
    }
}

From source file:Main.java

/**
 * This method performs a delta operation on two collections of elements; i.e. it returns a collection with elements that
 * are not found in the collection returns by the CollectionHelper.intersect() method.
 * The resulting collection guarantees unique membership as per implementation of the Java {@link java.util.Set} interface.
 * This is a null-safe operation.// ww w . j av  a  2 s. c om
 * @param firstGroup Collection to be used.
 * @param secondGroup Collection to be used.
 * @return The resulting collection which represents the delta of the two groups.
 */
public static <T> Collection<T> delta(Collection<T> groupOne, Collection<T> groupTwo) {

    if (groupOne == null)
        return groupTwo;
    else if (groupTwo == null)
        return groupOne;

    Set<T> delta = new HashSet<T>();
    delta.addAll(findGroupOneDelta(groupOne, groupTwo));
    delta.addAll(findGroupTwoDelta(groupTwo, groupOne));
    return delta;
}

From source file:Main.java

/**
 * Make UNION operation between 2 {@link Collection}
 * /*from  www  .  ja v a2  s .c om*/
 * @param a
 * @param b
 * @return
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Collection union(final Collection a, final Collection b) {
    ArrayList list = new ArrayList();
    Map mapa = getCardinalityMap(a);
    Map mapb = getCardinalityMap(b);
    Set elts = new LinkedHashSet(a);
    elts.addAll(b);
    Iterator it = elts.iterator();
    while (it.hasNext()) {
        Object obj = it.next();
        for (int i = 0, m = Math.max(getFreq(obj, mapa), getFreq(obj, mapb)); i < m; i++) {
            list.add(obj);
        }
    }
    return list;
}

From source file:Main.java

/**
 * Make INTERSECTION operation between 2 {@link Collection}
 * //from www.j av  a 2s .com
 * @param a
 * @param b
 * @return
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Collection intersection(final Collection a, final Collection b) {
    ArrayList list = new ArrayList();
    Map mapa = getCardinalityMap(a);
    Map mapb = getCardinalityMap(b);
    Set elts = new LinkedHashSet(a);
    elts.addAll(b);
    Iterator it = elts.iterator();
    while (it.hasNext()) {
        Object obj = it.next();
        for (int i = 0, m = Math.min(getFreq(obj, mapa), getFreq(obj, mapb)); i < m; i++) {
            list.add(obj);
        }
    }
    return list;
}