Example usage for java.util Set retainAll

List of usage examples for java.util Set retainAll

Introduction

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

Prototype

boolean retainAll(Collection<?> c);

Source Link

Document

Retains only the elements in this set that are contained in the specified collection (optional operation).

Usage

From source file:Main.java

public static <T> Set<T> intersect(Collection<? extends T> a, Collection<? extends T> b) {
    Set<T> intersection = new HashSet<T>(a);
    intersection.retainAll(b);
    return intersection;
}

From source file:Main.java

public static <E> Set<E> intersection(final Set<E> arg1, final Set<E> arg2) {

    Set<E> intersect = new HashSet<>(arg1);
    intersect.retainAll(arg2);
    return intersect;

}

From source file:Main.java

public static <T> Set<T> intersect(Set<T> s1, Set<T> s2) {
    Set<T> r = new HashSet<T>(s1);
    r.retainAll(s2);
    return r;// w  w  w.jav  a2  s .co  m
}

From source file:Main.java

/**
 * Intersection./*from  w w  w. j a  v  a  2s  .c om*/
 *
 * @param <T> the generic type
 * @param set1 the set1
 * @param set2 the set2
 * @return Uses the intersection paradigm. Non destructive
 */
public static <T> Set<T> intersection(Set<T> set1, Set<T> set2) {
    Set<T> tempSet = new HashSet<T>(set1);
    tempSet.retainAll(set2);
    return tempSet;
}

From source file:Main.java

/**
 * Returns intersection of two collections.
 *
 * @param <T> the generic type/*from w  ww  .j  a v  a 2  s  . c  om*/
 * @param coll1 the coll1
 * @param coll2 the coll2
 * @return the collection
 */
public static <T> Collection<T> intersect(Collection<T> coll1, Collection<T> coll2) {
    Set<T> intersection = new HashSet<T>(coll1);
    intersection.retainAll(new HashSet<T>(coll2));

    return intersection;
}

From source file:Main.java

public static double computeDice(Collection<String> c1, Collection<String> c2) {
    Set<String> intersection = new HashSet<>(c1);
    intersection.retainAll(c1);
    intersection.retainAll(c2);/*w w w  . j a va 2s  .  c  om*/

    if (intersection.size() == 0)
        return 0.0;
    double score = 2 * (double) intersection.size() / (c1.size() + c2.size());
    return score;

}

From source file:Main.java

/**
 * This method finds the intersection between set A and set B
 * @param setA set A/*from w  w w  .j a v a 2s . c  om*/
 * @param setB set B
 * @param <T> type
 * @return a new set containing the intersection
 */
public static <T> Set<T> intersection(Set<T> setA, Set<T> setB) {
    Set<T> intersection = new HashSet<T>(setA);
    intersection.retainAll(setB);
    return intersection;
}

From source file:Main.java

public static <T> Collection<T> intersection(final Collection<T> collection1, final Collection<T> collection2) {
    final Set<T> set = new HashSet<T>(collection1);
    set.retainAll(collection2);
    return set;/*w w  w  . j  av a 2  s .  c o  m*/
}

From source file:Main.java

public static <T> Set getDupplicatingValues(Set... sets) {
    Set<T> result = sets[0];
    for (int i = 1; i < sets.length; i++) {
        result.retainAll(sets[i]);
    }// w w  w . j  a  v  a 2  s  .  com

    return result;

}

From source file:SetOps.java

/**
 * /* w  w  w  . j a  v a 2 s. c  om*/
 * @param <T>
 * @param s1
 * @param s2
 * @return the intersection of s1 and s2. (The intersection of two sets is
 *         the set containing only the elements common to both sets.)
 */
public static <T> Set<T> intersection(Set<T> s1, Set<T> s2) {
    Set<T> intersection = new LinkedHashSet<T>(s1);
    intersection.retainAll(s2);
    return intersection;
}