Example usage for java.util LinkedHashSet LinkedHashSet

List of usage examples for java.util LinkedHashSet LinkedHashSet

Introduction

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

Prototype

public LinkedHashSet(Collection<? extends E> c) 

Source Link

Document

Constructs a new linked hash set with the same elements as the specified collection.

Usage

From source file:Main.java

/**
 * Make UNION operation between 2 {@link Collection}
 *
 * @param a/*from  w ww .ja v a2  s.  c o m*/
 * @param b
 * @return
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Collection union(final Collection a, final Collection b) {
    final ArrayList list = new ArrayList();
    final Map mapa = getCardinalityMap(a);
    final Map mapb = getCardinalityMap(b);
    final Set elts = new LinkedHashSet(a);
    elts.addAll(b);
    final Iterator it = elts.iterator();
    while (it.hasNext()) {
        final Object obj = it.next();
        for (int i = 0, m = Math.max(getFreq(obj, mapa), getFreq(obj, mapb)); i < m; i++) {
            list.add(obj);
        }
    }
    return list;
}

From source file:Main.java

/**
 * Given a list of elements of type <T>, remove the duplicates from the list in place
 * /*from  w  ww . ja va  2s.  com*/
 * @param <T>
 * @param list
 */
public static <T> void removeDuplicates(List<T> list) {
    // uses LinkedHashSet to keep the order
    Set<T> set = new LinkedHashSet<T>(list);

    list.clear();
    list.addAll(set);
    if (list instanceof ArrayList) {
        ((ArrayList<T>) list).trimToSize();
    }
}

From source file:Main.java

/**
 * Make INTERSECTION operation between 2 {@link Collection}
 *
 * @param a//from   ww  w .j av  a2 s.c  o m
 * @param b
 * @return
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Collection intersection(final Collection a, final Collection b) {
    final ArrayList list = new ArrayList();
    final Map mapa = getCardinalityMap(a);
    final Map mapb = getCardinalityMap(b);
    final Set elts = new LinkedHashSet(a);
    elts.addAll(b);
    final Iterator it = elts.iterator();
    while (it.hasNext()) {
        final Object obj = it.next();
        for (int i = 0, m = Math.min(getFreq(obj, mapa), getFreq(obj, mapb)); i < m; i++) {
            list.add(obj);
        }
    }
    return list;
}

From source file:Main.java

public static <T> LinkedHashSet<T> createLinkedHashSet(Iterable<? extends T> iter) {
    LinkedHashSet<T> set;/*from   w  ww . j  av a2s.  c  om*/

    if (iter instanceof Collection<?>) {
        set = new LinkedHashSet<T>((Collection<? extends T>) iter);
    } else {
        set = new LinkedHashSet<T>();
        iterableToCollection(iter, set);
    }

    return set;
}

From source file:SetOps.java

/**
 * /*from   w  w  w  .  j  av  a 2 s.c  o m*/
 * @param <T>
 * @param s1
 * @param s2
 * @return the union of s1 and s2. (The union of two sets is the set
 *         containing all of the elements contained in either set.)
 */
public static <T> Set<T> union(Set<T> s1, Set<T> s2) {
    Set<T> union = new LinkedHashSet<T>(s1);
    union.addAll(s2);
    return union;
}

From source file:Main.java

/**
 * Make UNION operation between 2 {@link Collection}
 * //  w w  w .  j  av  a  2s .c o m
 * @param a
 * @param b
 * @return
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Collection union(final Collection a, final Collection b) {
    ArrayList list = new ArrayList();
    Map mapa = getCardinalityMap(a);
    Map mapb = getCardinalityMap(b);
    Set elts = new LinkedHashSet(a);
    elts.addAll(b);
    Iterator it = elts.iterator();
    while (it.hasNext()) {
        Object obj = it.next();
        for (int i = 0, m = Math.max(getFreq(obj, mapa), getFreq(obj, mapb)); i < m; i++) {
            list.add(obj);
        }
    }
    return list;
}

From source file:Main.java

/**
 * Make INTERSECTION operation between 2 {@link Collection}
 * //w ww .j ava2 s  .  co m
 * @param a
 * @param b
 * @return
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Collection intersection(final Collection a, final Collection b) {
    ArrayList list = new ArrayList();
    Map mapa = getCardinalityMap(a);
    Map mapb = getCardinalityMap(b);
    Set elts = new LinkedHashSet(a);
    elts.addAll(b);
    Iterator it = elts.iterator();
    while (it.hasNext()) {
        Object obj = it.next();
        for (int i = 0, m = Math.min(getFreq(obj, mapa), getFreq(obj, mapb)); i < m; i++) {
            list.add(obj);
        }
    }
    return list;
}

From source file:Main.java

public static void removeDuplicates(List list) {
    Set set = new LinkedHashSet(list);
    list.clear();
    list.addAll(set);
}

From source file:Main.java

public static <T> Set<T> setOf(final Collection<T> c) {
    return new LinkedHashSet<T>(c);
}

From source file:Main.java

/**
 * Convert a varargs list of items into a Set while preserving order. An empty set is returned if items is null.
 * //from w w w .  j  a  v a 2 s . c  o  m
 * @param <T>
 *            Any type of object
 * @param items
 *            A variable length list of items of type T
 * @return Returns a new LinkedHashSet<T> or an empty set
 */
public static final <T> Set<T> newInOrderSet(T... items) {
    return addToSet(new LinkedHashSet<T>(items != null ? items.length : 0), items);
}