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 void getTop(float[] array, ArrayList<Integer> rankList, int i) {
    rankList.clear();/*from   www.j a va 2  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);
            // rankProbs.add(array[index]);
        }
        // System.out.println(m + "\t" + index);
    }
}

From source file:Main.java

public static <T> Set<T> intersection(Collection<? extends T> c1, Collection<? extends T> c2) {
    Set<T> result = new HashSet<T>();
    for (Iterator<? extends T> it = c1.iterator(); it.hasNext();) {
        T element = it.next();/*w ww.j a  va2  s  . c  om*/
        if (c2.contains(element))
            result.add(element);
    }
    return result;
}

From source file:Main.java

/**
 * Get only the unique elements in a collection
 *//*from w w  w. j  a  va 2 s.  c  om*/
public static Set<?> getUniqueElements(Collection<?> items) {
    Set<Object> uniqueSet = new HashSet<>();
    for (Object item : items) {
        uniqueSet.add(item);
    }

    return uniqueSet;
}

From source file:Main.java

/**
 * Get a new set//from  w  ww.ja  v a  2s.  c om
 * @param <T>
 * @return Set
 */
public static <T> Set<T> set() {
    return new HashSet<T>();
}

From source file:Main.java

@SuppressWarnings({ "rawtypes", "unchecked" })
public static List removeDuplicate(List list) {
    Set set = new HashSet();
    List newList = new ArrayList();
    for (Iterator iter = list.iterator(); iter.hasNext();) {
        Object element = iter.next();
        if (set.add(element))
            newList.add(element);//from w w w  .  j a va 2 s  . c om
    }
    //         list.addAll(newList);
    return newList;
}

From source file:Main.java

/**
 * Returns all the unique fields of the classes in the given list.
 * @author Tristan Bepler//from  ww  w.ja  v  a 2 s  .c o m
 */
private static Field[] getAllFields(List<Class<?>> classes) {
    Set<Field> fields = new HashSet<Field>();
    for (Class<?> clazz : classes) {
        fields.addAll(Arrays.asList(clazz.getDeclaredFields()));
    }
    return fields.toArray(new Field[fields.size()]);
}

From source file:Main.java

/**
 * @param sa a set//from   ww w . jav a2s .  c  o m
 * @param sb anotehr set
 * @return the intersection of <code>sa</code> and <code>sb</code>
 */
public static <E> Set<E> intersection(Set<E> sa, Set<E> sb) {

    Set<E> result = new HashSet<E>();
    if (sa == null || sb == null || sa.isEmpty() || sb.isEmpty())
        return result;

    Set<E> smallest = sa.size() < sb.size() ? sa : sb;
    Set<E> biggest = smallest == sa ? sb : sa;

    for (E entry : smallest) {
        if (biggest.contains(entry))
            result.add(entry);
    }

    return result;
}

From source file:Main.java

@SuppressWarnings({ "rawtypes", "unchecked" })
public static List removeDuplicateWithOrder(List list) {
    Set set = new HashSet();
    List newList = new ArrayList();
    for (Iterator iter = list.iterator(); iter.hasNext();) {
        Object element = iter.next();
        if (set.add(element))
            newList.add(element);//from w w w.jav  a2s . co m
    }
    return newList;
}

From source file:Main.java

public static <T> Set<T> unicon(Set<T> set1, Set<T> set2) {
    Set<T> set = new HashSet<>();
    set = set1;/* ww  w  .  j a v a2  s . c  o  m*/
    set.addAll(set2);
    return set;
}

From source file:Main.java

public static <V> HashSet<V> getHashSet(final Map<?, ? extends V> map,
        final Collection<? extends Object> keys) {
    final HashSet<V> result = new HashSet<V>();
    get(map, keys, result);/*from  ww  w .j  a va2s  .co m*/
    return result;
}