-2

How can you find the intersection of multiple (more than two) sets in Java?

retainAll by itself wont work since I need the ability to get the intersection between more than just two sets

stuart
  • 1,785
  • 2
  • 26
  • 38
  • Any code you can show us? – Sam M Jul 22 '15 at 17:58
  • @SamM oh, i posted this as an answer. I didn't realize I had to wait 2 days to accept my answer. I'm new to this stuff and this is my first time checking the "answer your own question" box. hope I'm not mis-using it. – stuart Jul 22 '15 at 18:01
  • why was this downvoted – stuart Jul 22 '15 at 18:04
  • 2
    the question looks like you didn't do any research before asking, this usually is a downvote-bait, but since you answered it yourself, this is wrong – Ringo Jul 24 '15 at 23:12
  • possible duplicate of [Is there a way to calculate the intersection of two sets?](http://stackoverflow.com/questions/8882097/is-there-a-way-to-calculate-the-intersection-of-two-sets) – ᴇʟᴇvᴀтᴇ Jul 24 '15 at 23:24

2 Answers2

1
public static <T> Collection<T> getIntersection(Collection<T>... sets) {

    Collection<T> firstSet;

    if (sets == null || sets.length == 0 || (firstSet = sets[0]) == null)
        return Collections.<T>emptySet();

    Collection<T> intersection = new HashSet(firstSet);

    for (Collection c : sets) {
        if (c == null) 
            return Collections.<T>emptySet();
        intersection.retainAll(c);
    }
    return intersection;
}
stuart
  • 1,785
  • 2
  • 26
  • 38
  • 1
    This may work, but it's reinventing the wheel. There is a built-in method `retainAll()` that does the same job. – ᴇʟᴇvᴀтᴇ Jul 24 '15 at 23:19
  • thanks @aetheria, i changed the answer to implement retainAll – stuart Jul 25 '15 at 15:32
  • 1
    2 other tips: if you use varargs, i don't think, that it (`sets`) can be `null`. You should check `sets.length != 0` and maybe a `null` check on the elements of `sets`. Also, It's a good practice, not to return `null`, when you promised to return a `Collection`! Return `Collections.emptyList()` or `Collections.emptySet()`, or throw an `Exception`, if you think, that a `null` value is a game breaker. – Balázs Édes Jul 25 '15 at 15:46
  • @bali182 cool, thank you! i edited per yr suggestions. also, it seems to work okay with a `null` `sets` input – stuart Jul 25 '15 at 17:06
  • Doesn't getIntersection() simply return what was passed to it? That's what it does when I use it. Say your first set in the passed collection is (11,12) and other sets are (1,3,4,5), etc and not containing any elements contained in the first set, then wouldn't using retainAll() result in an empty result set? As the javaDoc for retainall() states "...removes from this set all of its elements that are not contained in the specified collection..." so if there is no match for the first 2 sets then all subsequent uses of retainAll() will result in an empty set. – Graham Seed Jul 28 '17 at 16:18
1

You can use the Set's retainAll(other) method which retains only items that are in both sets. It alters the original set, so you may want to take a copy of the set first (use the appropriate constructor).

ᴇʟᴇvᴀтᴇ
  • 12,285
  • 4
  • 43
  • 66