Example usage for java.util Collection retainAll

List of usage examples for java.util Collection retainAll

Introduction

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

Prototype

boolean retainAll(Collection<?> c);

Source Link

Document

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

Usage

From source file:Main.java

public static <T> Collection<T> intersect(Collection<T> col1, Collection<T> col2) {
    Collection<T> col1Clone = clone(col1);
    col1Clone.retainAll(col2);
    return col1Clone;
}

From source file:Main.java

public static Collection<? extends Object> getIntersection(Collection<? extends Object> list1,
        Collection<? extends Object> list2) {
    HashSet<?> hashSet = new HashSet<Object>(list1);
    Collection<?> intersection = hashSet;
    intersection.retainAll(list2);
    return intersection;
}

From source file:Main.java

/**
 * Returns all items that are both in collection1 and in collection2
 *//*from   w  ww. j av a  2 s  .  c  o  m*/
public static <T> Collection<T> intersectCollections(Collection<? extends T> collection1,
        Collection<? extends T> collection2) {
    Collection<T> result = new ArrayList<T>(collection1);
    result.retainAll(collection2);
    return result;
}

From source file:Main.java

/**
 *
 * @param container a collection/*from  w  w  w.ja  v a2  s.co m*/
 * @param contained another collection
 * @return an intersection
 */
public static Collection intersect(Collection container, Collection contained) {

    Collection copy = new ArrayList(container);
    copy.retainAll(contained);

    return copy;
}

From source file:Main.java

public static <T> Collection<? extends T> maskedCopyOf(final Collection<? extends T> source,
        final Collection<? extends T> mask) {
    final Collection<T> copy = new LinkedList<T>(source);
    copy.retainAll(new ArrayList<T>(mask));
    return copy;/*from   w  w  w  . j  a v a2  s  .  c o  m*/
}

From source file:Main.java

public static <E> void intersection(final Collection<E> a, final Collection<E> b) {
    if (b != null && !b.isEmpty()) {
        if (a.isEmpty()) {
            a.addAll(b);/*  ww  w. j  a  v a 2  s .  co  m*/
        } else {
            a.retainAll(b);
        }
    }
}

From source file:Main.java

/**
 * //from   w ww  .j  ava2  s .  c om
 * @param collections
 * @return Collection
 */
public static Collection<?> getIntersection(
        final Collection<? extends Collection<? extends Object>> collections) {
    final List<Collection<?>> collectionsList = new ArrayList<Collection<?>>(collections);
    final Collection<Object> intersection = new HashSet<>();

    if (collectionsList.size() != 0) {
        intersection.addAll(collectionsList.get(0));
    }

    for (int i = 1; i < collections.size() && intersection.size() > 0; i++) {
        intersection.retainAll(new HashSet<>(collectionsList.get(i)));
    }

    return intersection;
}

From source file:org.onehippo.forge.solr.indexer.task.JcrUtils.java

/**
 * Check if a node is of a certain type//w w  w  .j  ava  2 s  . co m
 * @param node Node
 * @param types Types
 * @return TRUE if the node matches with one of the types
 */
public static boolean isOfType(Node node, Collection<String> types) {
    if (node == null || types == null || types.isEmpty()) {
        return false;
    }
    Collection<String> intersect = new ArrayList<String>(types);
    intersect.retainAll(getNodeTypes(node));
    return !intersect.isEmpty();
}

From source file:nl.strohalm.cyclos.services.preferences.PreferenceServiceSecurity.java

private static <T> Set<T> filterAllowed(final Collection<T> elements, final Collection<T> allowed) {
    if (CollectionUtils.isEmpty(allowed)) {
        return Collections.emptySet();
    }/*ww  w  .ja v  a 2 s .c  o m*/
    if (CollectionUtils.isEmpty(elements)) {
        return Collections.emptySet();
    }
    elements.retainAll(allowed);
    return new HashSet<T>(elements);
}

From source file:org.apache.hyracks.algebricks.core.algebra.operators.logical.visitors.VariableUtilities.java

public static void getSubplanLocalLiveVariables(ILogicalOperator op, Collection<LogicalVariable> liveVariables)
        throws AlgebricksException {
    VariableUtilities.getLiveVariables(op, liveVariables);
    Set<LogicalVariable> locallyProducedVars = new HashSet<>();
    VariableUtilities.getProducedVariablesInDescendantsAndSelf(op, locallyProducedVars);
    liveVariables.retainAll(locallyProducedVars);
}