Java Utililty Methods Set Intersect

List of utility methods to do Set Intersect

Description

The list of methods to do Set Intersect are organized into topic(s).

Method

SetgetIntersection(Set a, Set b)
get Intersection
if (a == null || b == null) {
    return Collections.EMPTY_SET;
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;
...
SetgetIntersection(Set s1, Set s2)
Returns the intersection of set s1 and set s2.
if (s1 == null || s2 == null)
    return new HashSet<T>();
Set<T> result = new HashSet<T>(s1);
result.retainAll(s2);
return result;
Collectionintersect(Collection set1, Collection set2)
intersect
if (isEmpty(set1) || isEmpty(set2)) {
    return Collections.emptyList();
Set<T> intersection = new LinkedHashSet<T>();
for (T item : set1) {
    if (item != null && set2.contains(item)) {
        intersection.add(item);
return intersection;
Setintersect(final Set firstSet, final Set secondSet)
Utility method returning the intersection between the given sets (i.e.
if (isEmpty(firstSet)) {
    return secondSet == null ? new HashSet<T>(0) : secondSet;
if (isEmpty(secondSet)) {
    return firstSet;
firstSet.retainAll(secondSet);
return firstSet;
...
Setintersect(Set one, Set two)
Form a new set that is the intersection of one set with another set.
HashSet n = new HashSet(one.size());
Iterator it = one.iterator();
while (it.hasNext()) {
    Object v = it.next();
    if (two.contains(v)) {
        n.add(v);
return n;
Setintersect(Set set1, Set set2)
Perform the operation intersect on two given sets.
Set<T> s = set();
intersect(set1, set2, s);
return s;
Setintersect(Set a, Set b)
intersect
Set<E> intersect = new HashSet<>();
for (E entryA : a) {
    if (b.contains(entryA)) {
        intersect.add(entryA);
for (E entryB : b) {
    if (a.contains(entryB)) {
...
booleanintersect(Set a, Set b)
Checks that there is at least one matching item between the two sets.
for (String s : a) {
    if (b.contains(s))
        return true;
return false;
SetintersectComparable(Set left, Set right)
intersect Comparable
List<T> mid = new LinkedList<T>(left);
mid.removeAll(right);
Set<T> result = new TreeSet<T>(left);
result.removeAll(mid);
return result;
Setintersection(final Set set1, final Set set2)
intersection
Set<E> set = new HashSet<>(set1);
set.retainAll(set2);
return Collections.unmodifiableSet(set);