intersection two Collection - Java java.util

Java examples for java.util:Collection Intersect

Description

intersection two Collection

Demo Code


//package com.book2s;
import java.util.*;

public class Main {
    public static void main(String[] argv) {
        Collection a = java.util.Arrays.asList("asdf", "book2s.com");
        Collection b = java.util.Arrays.asList("asdf", "book2s.com");
        System.out.println(intersection(a, b));
    }//from   ww w .  j  a v a 2 s . c om

    public static <T> List intersection(Collection<T> a, Collection<T> b) {
        List<T> intersection = new ArrayList<T>();
        if (a != null && b != null) {
            for (T o : a) {
                if (b.contains(o))
                    intersection.add(o);
            }
        }
        return intersection;
    }
}

Related Tutorials