Java Collection Intersect intersectionSize(Collection c1, Collection c2)

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

Description

Calculate the size of intersection between two collections.

License

Open Source License

Parameter

Parameter Description
c1 a collection
c2 a collection

Return

the size of intersection

Declaration

public static int intersectionSize(Collection c1, Collection c2) 

Method Source Code

//package com.java2s;
/* Copyright (c) 2010, Johannes K?ster <johannes.koester@tu-dortmund.de>
 * All rights reserved./*from  w w  w.  ja  va  2  s. co  m*/
 *
 * This software is open-source under the BSD license; see "license.txt"
 * for a description.
 */

import java.util.Collection;

public class Main {
    /**
     * Calculate the size of intersection between two collections.
     *
     * @param c1 a collection
     * @param c2 a collection
     * @return the size of intersection
     */
    public static int intersectionSize(Collection c1, Collection c2) {
        int size = 0;
        if (c1.size() < c2.size()) {
            for (Object o : c1) {
                if (c2.contains(o)) {
                    size++;
                }
            }

        } else {
            for (Object o : c2) {
                if (c1.contains(o)) {
                    size++;
                }
            }
        }
        return size;
    }
}

Related

  1. intersection(Collection values1, Collection values2)
  2. intersection(final Collection a, final Collection b)
  3. intersection(final Collection c1, final Collection c2)
  4. intersection(final Collection a, final Collection b)
  5. intersection(final Collection c1, final Collection c2)
  6. intersects(Collection c1, Collection c2)
  7. intersects(Collection c0, Collection c1)
  8. intersectsWith(Collection S1, Collection S2)
  9. makeIntersection(Collection a, Collection b)