intersect two Collections - Java java.util

Java examples for java.util:Collection Intersect

Description

intersect two Collections

Demo Code


//package com.java2s;

import java.util.Collection;

import java.util.HashSet;

import java.util.Set;

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

    public static <T> Collection<T> intersect(Collection<T> a,
            Collection<T> b) {
        Set<T> set = new HashSet<T>(a);
        set.retainAll(b);
        return set;
    }
}

Related Tutorials