Example usage for java.util HashSet HashSet

List of usage examples for java.util HashSet HashSet

Introduction

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

Prototype

public HashSet(int initialCapacity) 

Source Link

Document

Constructs a new, empty set; the backing HashMap instance has the specified initial capacity and default load factor (0.75).

Usage

From source file:Main.java

public static <T> boolean hasDuplicates(Collection<? extends T> xs) {
    HashSet<T> ys = new HashSet<>(xs.size());

    for (T x : xs)
        if (!ys.add(x))
            return true;

    return false;
}

From source file:Main.java

public static <T> List<T> removeDuplication(List<T> list) {
    return new ArrayList<T>(new HashSet<T>(list));
}

From source file:Main.java

public static <E> Set<E> set(E... elements) {
    return new HashSet<E>(Arrays.asList(elements));
}

From source file:Main.java

public static <T> Collection<T> diff(Collection<T> a, Collection<T> b) {
    Set<T> set = new HashSet<T>(a);
    set.removeAll(b);//from   w w  w  .  j a v  a2s  .  co m
    return set;
}

From source file:Main.java

public static <T> Collection<T> union(Collection<T> a, Collection<T> b) {
    Set<T> set = new HashSet<T>(a);
    for (T obj : b) {
        if (!set.contains(obj)) {
            set.add(obj);// w  ww. j a  va  2 s . co m
        }
    }
    return set;
}

From source file:Main.java

/**
 * @param set set//from w ww  .  j  a  v  a  2s.  co  m
 * @return set deep copy
 */
public static Set<String> cloneSet(Set<String> set) {
    return new HashSet<>(set);
}

From source file:Main.java

public static <E> Set<E> intersection(final Set<E> arg1, final Set<E> arg2) {

    Set<E> intersect = new HashSet<>(arg1);
    intersect.retainAll(arg2);/*from  ww w  . j  a v  a  2 s .co  m*/
    return intersect;

}

From source file:Main.java

public static Set intersection(Set one, Set two) {
    Set big, small;//  w w w. j  a  v  a2s.  co  m
    if (one.size() >= two.size()) {
        big = new HashSet(one);
        small = two;
    } else {
        big = new HashSet(two);
        small = one;
    }
    big.removeAll(small);
    return big;
}

From source file:Main.java

public static <T extends Object> Set<T> toSet(T[] arr) {
    Set<T> tSet = new HashSet<T>(Arrays.asList(arr));
    return tSet;//from   w w  w .  j av  a2  s .co  m

}

From source file:Main.java

/**
 * Remove the value from collection coll and return a new collection.
 *//*from  w w  w.ja  va  2s .  c  o m*/
public static <T> Set<T> remove(Collection<T> coll, T value) {
    Set<T> copy = new HashSet<T>(coll);
    copy.remove(value);
    return copy;
}