Java Set Intersect intersect(Set a, Set b)

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

Description

Checks that there is at least one matching item between the two sets.

License

Apache License

Parameter

Parameter Description
a First set to check
b Second set to check

Return

Returns true if there is at lease one matching item between the two sets.

Declaration

public static boolean intersect(Set<String> a, Set<String> b) 

Method Source Code


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

import java.util.*;

public class Main {
    /**//from   ww w . java 2 s  .  com
     * Checks that there is at least one matching item between the two sets.
     *
     * @param a First set to check
     * @param b Second set to check
     * @return Returns true if there is at lease one matching item between the two sets.
     */
    public static boolean intersect(Set<String> a, Set<String> b) {
        for (String s : a) {
            if (b.contains(s))
                return true;
        }
        return false;
    }

    public static boolean contains(Set<String> setTocheck, Set<String> mustHaveAllThese) {

        for (String s : mustHaveAllThese) {//iterate over all must have tags.
            if (!setTocheck.contains(s)) {//return immediately when item not found in setToCheck
                return false;
            }
        }
        return true;
    }
}

Related

  1. intersect(Collection set1, Collection set2)
  2. intersect(final Set firstSet, final Set secondSet)
  3. intersect(Set one, Set two)
  4. intersect(Set set1, Set set2)
  5. intersect(Set a, Set b)
  6. intersectComparable(Set left, Set right)
  7. intersection(final Set set1, final Set set2)
  8. intersection(Set a, Set b)
  9. intersection(Set sa, Set sb)