Java Collection Intersect intersect(Collection collA, Collection collB, Collection target)

Here you can find the source of intersect(Collection collA, Collection collB, Collection target)

Description

return a new Collection with the elements which appear both in a and b.
Complexity: for sets - linear, for lists - quadratic.

License

Open Source License

Parameter

Parameter Description
T a parameter
collA a parameter
collB a parameter
target a collection that will be returned by this method. It will be cleared by this method before inserting the required elements.

Return

a new Collection with the elements which appear both in a and b

Declaration

public static <T> Collection<T> intersect(Collection<T> collA, Collection<T> collB, Collection<T> target) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Collection;

public class Main {
    /**//from  w  w w .j  av a2 s  .com
     * return a new Collection with the elements which appear both in a and b.<BR>
     * <B>Complexity:</B> for sets - linear, for lists - quadratic.
     * 
     * @param <T>
     * @param collA
     * @param collB
     * @param target a collection that will be returned by this method. It will be cleared by this method before inserting the required elements. 
     * @return a new Collection with the elements which appear both in a and b 
     */
    public static <T> Collection<T> intersect(Collection<T> collA, Collection<T> collB, Collection<T> target) {
        target.clear();

        for (T elem : collA) {
            if (collB.contains(elem)) {
                target.add(elem);
            }
        }
        return target;
    }
}

Related

  1. intersect(Collection c1, Collection c2)
  2. intersect(Collection c1, Collection c2)
  3. intersect(Collection collection, Collection otherCollection)
  4. intersect(Collection... collections)
  5. intersect(Collection a, Collection b)
  6. intersect(Collection collection1, Collection collection2)
  7. intersect(Collection t1, Collection t2)
  8. intersectCollections(Collection collection1, Collection collection2)
  9. intersectingCollections(Collection collection1, Collection collection2)