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

public static Set<Node> findElementsByTag(Set<Node> nodes, String tag) {
    final Set<Node> result = new HashSet<>();

    for (Node child : nodes) {
        if (tag.equals(child.getNodeName())) {
            result.add(child);/*from w w  w. ja  v a 2  s . co m*/
        }
    }

    return result;
}

From source file:Main.java

/**
 *  Returns a set containing the passed elements.
 *//*from  w  w w.java  2 s  .c o m*/
public static <T> Set<T> asSet(T... elems) {
    Set<T> result = new HashSet<T>();
    for (T elem : elems)
        result.add(elem);
    return result;
}

From source file:Main.java

public static Collection intersection(Collection a, Collection b)

{

    Collection results = new HashSet();

    for (Iterator i = a.iterator(); i.hasNext();) {

        Object o = i.next();/*from  ww w. ja v a2 s  . co m*/

        if (b.contains(o)) {

            results.add(o);

        }

    }

    return results;

}

From source file:Main.java

/**
 * <p>//from   ww  w  .j  a  va  2 s .co  m
 *    Creates an empty Set of String types.
 * </p>
 * 
 * @return Set of String types
 */
public static Set<String> CreateEmptyStringSet() {
    return ((Set<String>) (new HashSet<String>()));
}

From source file:Main.java

public static Set<Long> getLongValues(String[] parameterValues, String message) {
    Set<Long> ids = null;
    if (parameterValues != null && parameterValues.length > 0) {
        ids = new HashSet<>();
        for (String parameter : parameterValues) {
            try {
                Long id = Long.parseLong(parameter);
                if (id > 0) {
                    ids.add(Long.parseLong(parameter));
                }//from  ww  w .  jav a  2 s.c o  m
            } catch (NumberFormatException e) {
                throw new IllegalArgumentException(message);
            }
        }
    }
    return ids;
}

From source file:Main.java

public static void getTop(double[] array, ArrayList<Integer> rankList, int i) {
    int index = 0;
    HashSet<Integer> scanned = new HashSet<Integer>();
    double max = Double.MIN_VALUE;
    for (int m = 0; m < i && m < array.length; m++) {
        max = Double.MIN_VALUE;//ww  w. ja va 2 s.co  m
        for (int no = 0; no < array.length; no++) {
            if (!scanned.contains(no)) {
                if (array[no] > max) {
                    index = no;
                    max = array[no];
                } else if (array[no] == max && Math.random() > 0.5) {
                    index = no;
                    max = array[no];
                }
            }
        }
        if (!scanned.contains(index)) {
            scanned.add(index);
            rankList.add(index);
        }
        //System.out.println(m + "\t" + index);
    }
}

From source file:Main.java

public static <T, V extends T> HashSet<T> createHashSet(V... args) {
    if ((args == null) || (args.length == 0)) {
        return new HashSet();
    }/*from   w w w.  j  av  a  2  s .  c o  m*/
    HashSet<T> set = new HashSet(args.length);
    for (V v : args) {
        set.add(v);
    }
    return set;
}

From source file:Main.java

/**
 * <p>Turn source list to Set</p>
 * @param list source list/*from   w  ww  . jav  a2 s. c o m*/
 * @return
 */
public static <T extends Object> Set<T> turnListToSet(List<T> list) {
    Set set = new HashSet<T>();
    if (list != null) {
        for (T obj : list) {
            set.add(obj);
        }
    }
    return set;
}

From source file:Main.java

/**
 * Build a set from any {@link Iterator}.
 * @param it/*from   w  ww  .  jav  a 2s .  c  o m*/
 * @return
 */
public static <E> Set<E> buildSetFromIterator(final Iterator<E> it) {
    final Set<E> set = new HashSet<>();

    while (it.hasNext()) {
        set.add(it.next());
    }

    return set;
}

From source file:Main.java

public static void getTop(float[] array, ArrayList<Integer> rankList, int i) {
    rankList.clear();/*from   www .  jav a2  s.  c o  m*/

    int index = 0;
    HashSet<Integer> scanned = new HashSet<Integer>();
    float max = Float.MIN_VALUE;
    for (int m = 0; m < i && m < array.length; m++) {
        boolean flag = false;
        max = Float.MIN_VALUE;
        for (int no = 0; no < array.length; no++) {
            if (!scanned.contains(no) && array[no] >= max) {
                index = no;
                max = array[no];
                flag = true;
            }
        }
        if (flag) { // found value
            scanned.add(index);
            rankList.add(index);
        }
    }
}