intersect two Collection - Android java.util

Android examples for java.util:Collection Intersect

Description

intersect two Collection

Demo Code


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

import java.util.HashSet;

import java.util.Set;

public class Main {
    public static void main(String[] argv) {
        Collection set1 = java.util.Arrays.asList("asdf", "book2s.com");
        Collection set2 = java.util.Arrays.asList("asdf", "book2s.com");
        System.out.println(intersect(set1, set2));
    }/*w  w  w  .  j  a  v  a2  s.c  om*/

    public static <T> Set<T> intersect(Collection<T> set1,
            Collection<T> set2) {
        HashSet<T> set = new HashSet<T>();

        for (T object : set1) {
            if (set2.contains(object)) {
                set.add(object);
            }
        }

        return set;
    }
}

Related Tutorials