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 removeDuplicateWithOrder(List arlList) {
    Set set = new HashSet();
    List newList = new ArrayList();
    for (Iterator iter = arlList.iterator(); iter.hasNext();) {
        Object element = iter.next();
        if (set.add(element))
            newList.add(element);/*from w ww.  ja v  a  2s.  c o m*/
    }
    arlList.clear();
    arlList.addAll(newList);
}

From source file:Main.java

public static String[] union(String[] arr1, String[] arr2) {
    Set<String> set = new HashSet<>();
    Collections.addAll(set, arr1);
    Collections.addAll(set, arr2);
    String[] result = {};/*from w  ww.  j  a  va 2  s  . co  m*/
    return set.toArray(result);
}

From source file:Main.java

@SafeVarargs
public static <T> HashSet<T> newHashSet(T... ts) {
    HashSet<T> set = new HashSet<T>();
    for (T t : ts) {
        set.add(t);/* w  w  w  .j  a va2  s.c om*/
    }
    return set;
}

From source file:Main.java

public static <T> Set<T> toSet(Set<T> set, T... arr) {
    if (set == null) {
        set = new HashSet<T>();
    }/*w  w w  . j  ava 2 s . c  o  m*/

    return (Set<T>) copy(set, arr);
}

From source file:Main.java

public static <T> T getFirstDuplicateValue(Collection<T> collection) {
    Set<T> set = new HashSet<T>();
    // Set#add returns false if the set does not change, which
    // indicates that a duplicate element has been added.
    for (T each : collection) {
        if (!set.add(each))
            return each;
    }//  ww  w.jav a2  s  .co m
    return null;
}

From source file:Main.java

public static void filterSets() {
    Set<String> words = new HashSet<>();
    words.add("1");
    words.add("2");
    words.add("3");
    Set<String> filteredSet = Sets.filter(words, new Predicate<String>() {
        @Override/*from ww w .  j a va2s .  co m*/
        public boolean apply(String input) {
            return input.equals("1");
        }
    });

    System.out.println(Iterables.toString(filteredSet));

}

From source file:Main.java

/**
 * @param <T>/*ww w .  j  av  a 2 s.c  o m*/
 * @param list
 * @return list
 */
public static <T> Set<T> list2Set(List<T> list) {
    Set<T> set = new HashSet<T>();
    for (T t : list) {
        set.add(t);
    }
    return set;
}

From source file:Main.java

public static <T> List<T> union(List<T> list1, List<T> list2) {
    Set<T> set = new HashSet<T>();

    set.addAll(list1);// w  ww.j a v  a2 s.c  o  m
    set.addAll(list2);

    return new ArrayList<T>(set);
}

From source file:Main.java

public static <T> Set<String> toStringSet(Collection<T> a_values)
/*     */ {/*from  w  w w  . j  a  v a  2 s .c o m*/
    /* 443 */Set values = new HashSet();
    /*     */
    /* 445 */for (Iterator i$ = a_values.iterator(); i$.hasNext();) {
        Object value = i$.next();
        /*     */
        /* 447 */values.add(value.toString());
        /*     */}
    /*     */
    /* 450 */return values;
    /*     */}

From source file:Main.java

public static <T> Set<Class<? extends T>> filter(Collection<Class<? extends T>> source, String packageFilter) {
    Set<Class<? extends T>> filtered = new HashSet<Class<? extends T>>();
    for (Class<? extends T> klazz : source) {
        if (klazz.getName().startsWith(packageFilter))
            filtered.add(klazz);/* w  w  w.j  a v  a2s . c  o  m*/
    }
    return filtered;
}