Java Collection Intersect intersect(Collection c1, Collection c2)

Here you can find the source of intersect(Collection c1, Collection c2)

Description

Computes the intersection between two collections

License

Open Source License

Parameter

Parameter Description
c1 the first collection
c2 the second (obviously) collection

Declaration

public static Collection intersect(Collection c1, Collection c2) 

Method Source Code

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

import java.util.*;

public class Main {
    /**//from  w w  w .  j a v  a 2s  .  c  o m
     * Computes the intersection between two collections
     *
     * @param c1 the first collection
     * @param c2 the second (obviously) collection
     */
    public static Collection intersect(Collection c1, Collection c2) {
        return intersect(c1, c2, null);
    }

    /**
     * Computes the intersection between two collections
     *
     * @param c1         the first collection
     * @param c2         the second (obviously) collection
     * @param comparator is used to determine if two items are the same
     */
    public static Collection intersect(Collection c1, Collection c2,
            Comparator comparator) {

        Collection result = new LinkedList();

        if (c1 == null || c2 == null || c1.size() == 0 || c2.size() == 0)
            return result;

        /*
         * This algorithm is shamelessly stolen from Collections.disjoint() ...
         * 
         * We're going to iterate through c1 and test for inclusion in c2. If c1 is
         * a Set and c2 isn't, swap the collections. Otherwise, place the shorter
         * collection in c1. Hopefully this heuristic will minimize the cost of the
         * operation.
         */
        Collection left = c1;
        Collection right = c2;
        if ((left instanceof Set) && !(right instanceof Set)
                || (left.size() > right.size())) {

            left = c2;
            right = c1;
        }

        for (Object l : left) {
            if (contains(right, l, comparator))
                result.add(l);
        }
        return result;
    }

    /**
     * checks whether the object o is contained in the collection c
     *
     * @param comparator if not null, used to determine if two items are the same
     */
    public static boolean contains(Collection c, Object o,
            Comparator comparator) {
        if (comparator == null) {
            // simply check if 'c' contains the pointer 'o'
            return c.contains(o);
        } else {
            // look into 'c' for occurence of 'o'
            for (Object o2 : c) {
                if (comparator.compare(o, o2) == 0) { // the objects match
                    return true;
                }
            }
        }
        return false;
    }
}

Related

  1. hasIntersection(Collection a, Collection b)
  2. hasIntersection(Collection c1, Collection c2)
  3. hasIntersection(Collection col1, Collection col2)
  4. hasIntersection(final Collection a, final Collection b)
  5. intersect(Collection c1, Collection c2)
  6. intersect(Collection collection, Collection otherCollection)
  7. intersect(Collection... collections)
  8. intersect(Collection a, Collection b)
  9. intersect(Collection collA, Collection collB, Collection target)