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() 

Source Link

Document

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

Usage

From source file:Main.java

private static Set<Character> strToSet(final String str) {
    Set<Character> set;

    if (str == null) {
        return new HashSet<Character>();
    }/*from   ww w. j  a  va  2s  . com*/
    set = new HashSet<Character>(str.length());
    for (int i = 0; i < str.length(); i++) {
        set.add(str.charAt(i));
    }
    return set;
}

From source file:Main.java

public static void uniqe(ArrayList<String> queryDoc) {
    // add elements to al, including duplicates
    HashSet<String> hs = new HashSet<String>();
    hs.addAll(queryDoc);// w ww.  j  ava  2  s.  c  om
    queryDoc.clear();
    queryDoc.addAll(hs);
}

From source file:Main.java

public static <T> Set<T> hashSetOf(T... ts) {
    Set<T> results = new HashSet<>();
    Collections.addAll(results, ts);
    return results;
}

From source file:Main.java

public static HashSet<String> intersectionSet(HashSet<String> setA, HashSet<String> setB) {
    HashSet<String> intersectionSet = new HashSet<String>();
    Iterator<String> iterA = setA.iterator();
    while (iterA.hasNext()) {
        String tempInner = iterA.next();
        if (setB.contains(tempInner)) {
            intersectionSet.add(tempInner);
        }/*from w w  w  . ja va  2s .c om*/
    }
    return intersectionSet;
}

From source file:Main.java

public static Set<Integer> getIntersection(Set<Integer> set1, Set<Integer> set2) {
    Set<Integer> ret = new HashSet<Integer>();
    for (Integer i : set1) {
        if (set2.contains(i)) {
            ret.add(i);/* w  w  w  . j av  a2  s. co  m*/
        }
    }
    return ret;
}

From source file:Main.java

public static Integer[] randomIntArray(int size) {

    Integer a[] = new Integer[size];
    HashSet<Integer> set = new HashSet<>();
    for (int x = 0; x < a.length; x++) {

        Random r = new Random();
        Integer i = r.nextInt(size * 10);

        while (set.contains(i)) {
            i = r.nextInt(size);//from  w w w . j  av  a  2 s. c  o  m
        }
        set.add(i);
        a[x] = i;
    }
    return a;
}

From source file:Main.java

public static <T> HashSet<T> toHashSet(T[] items) {
    if (items == null || items.length == 0)
        return null;
    HashSet<T> set = new HashSet<>();
    Collections.addAll(set, items);
    return set;/*from w  w  w .  ja  v a  2  s.  com*/
}

From source file:Main.java

@SafeVarargs
public static <T> HashSet<T> newHashSet(T... ts) {
    HashSet<T> set = new HashSet<>();
    for (T t : ts) {
        set.add(t);//  w  w w  .  ja va 2  s .  c  o m
    }
    return set;
}

From source file:Main.java

public static Collection<InetAddress> getAllAvailableAddresses() {
    Set<InetAddress> retval = new HashSet<InetAddress>();
    Enumeration en;//from  ww  w .  j av a  2  s.  c  o  m

    try {
        en = NetworkInterface.getNetworkInterfaces();
        if (en == null)
            return retval;
        while (en.hasMoreElements()) {
            NetworkInterface intf = (NetworkInterface) en.nextElement();
            Enumeration<InetAddress> addrs = intf.getInetAddresses();
            while (addrs.hasMoreElements())
                retval.add(addrs.nextElement());
        }
    } catch (SocketException e) {
        e.printStackTrace();
    }

    return retval;
}

From source file:Main.java

public static <T> HashSet<T> newHashSet(T... ts) {
    HashSet<T> set = new HashSet<T>();
    for (T t : ts) {
        set.add(t);/*from w  w w  .  jav  a  2 s.  c o m*/
    }
    return set;
}