Java Set Intersect intersection(Set sa, Set sb)

Here you can find the source of intersection(Set sa, Set sb)

Description

intersection

License

Apache License

Parameter

Parameter Description
sa a set
sb anotehr set

Return

the intersection of sa and sb

Declaration

public static <E> Set<E> intersection(Set<E> sa, Set<E> sb) 

Method Source Code


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

import java.util.*;

public class Main {
    /**//w  ww . j  a v a2s. co m
     * @param sa a set
     * @param sb anotehr set
     * @return the intersection of <code>sa</code> and <code>sb</code>
     */
    public static <E> Set<E> intersection(Set<E> sa, Set<E> sb) {

        Set<E> result = new HashSet<E>();
        if (sa == null || sb == null || sa.isEmpty() || sb.isEmpty())
            return result;

        Set<E> smallest = sa.size() < sb.size() ? sa : sb;
        Set<E> biggest = smallest == sa ? sb : sa;

        for (E entry : smallest) {
            if (biggest.contains(entry))
                result.add(entry);
        }

        return result;
    }
}

Related

  1. intersect(Set a, Set b)
  2. intersect(Set a, Set b)
  3. intersectComparable(Set left, Set right)
  4. intersection(final Set set1, final Set set2)
  5. intersection(Set a, Set b)
  6. intersection(Set first, Set second)
  7. intersection(Set s1, Set s2)
  8. intersection(Set setA, Set setB)
  9. intersection(Set setA, Set setB)