Java Set Intersect getIntersection(Set a, Set b)

Here you can find the source of getIntersection(Set a, Set b)

Description

get Intersection

License

Apache License

Declaration

public static <T> Set<T> getIntersection(Set<T> a, Set<T> b) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Collections;

import java.util.HashSet;

import java.util.Set;

public class Main {
    public static <T> Set<T> getIntersection(Set<T> a, Set<T> b) {
        if (a == null || b == null) {
            return Collections.EMPTY_SET;
        }//from  w w w. ja  v  a  2 s  .c  o  m

        Set<T> small = (a.size() > b.size()) ? b : a;
        Set<T> big = (a.size() > b.size()) ? a : b;

        Set<T> intersection = new HashSet<T>(small);
        intersection.retainAll(big);
        return intersection;
    }
}

Related

  1. getIntersection(Set s1, Set s2)
  2. intersect(Collection set1, Collection set2)
  3. intersect(final Set firstSet, final Set secondSet)
  4. intersect(Set one, Set two)