count Common Elements - Java java.util

Java examples for java.util:Collection Compare

Description

count Common Elements

Demo Code


//package com.java2s;
import java.util.Collection;

public class Main {
    public static void main(String[] argv) {
        Collection c1 = java.util.Arrays.asList("asdf", "java2s.com");
        Collection c2 = java.util.Arrays.asList("asdf", "java2s.com");
        System.out.println(countCommonElements(c1, c2));
    }/*ww  w.jav  a  2  s . c  om*/

    public static <T> int countCommonElements(Collection<T> c1,
            Collection<T> c2) {
        int nbCommon = 0;
        for (T e : c1) {
            if (c2.contains(e)) {
                nbCommon++;
            }
        }
        return nbCommon;
    }
}

Related Tutorials