Something like 'contains any' for Java set?

Java

Java Problem Overview


I have two sets, A and B, of the same type.

I have to find if A contains any element from the set B.

What would be the best way to do that without iterating over the sets? The Set library has contains(object) and containsAll(collection), but not containsAny(collection).

Java Solutions


Solution 1 - Java

Wouldn't Collections.disjoint(A, B) work? From the documentation:

> Returns true if the two specified collections have no elements in common.

Thus, the method returns false if the collections contains any common elements.

Solution 2 - Java

Stream::anyMatch

Since Java 8 you could use Stream::anyMatch.

setA.stream().anyMatch(setB::contains)

Solution 3 - Java

Apache Commons has a method CollectionUtils.containsAny().

Solution 4 - Java

A good way to implement containsAny for sets is using the Guava Sets.intersection().

containsAny would return a boolean, so the call looks like:

Sets.intersection(set1, set2).isEmpty()

This returns true iff the sets are disjoint, otherwise false. The time complexity of this is likely slightly better than retainAll because you dont have to do any cloning to avoid modifying your original set.

Solution 5 - Java

I use org.apache.commons.collections.CollectionUtils

CollectionUtils.containsAny(someCollection1, someCollection2)

That is All! Returns true if at least one element is in both collections.

Simple to use, and the name of the function is more suggestive.

Solution 6 - Java

Use retainAll() in the Set interface. This method provides an intersection of elements common in both sets. See the API docs for more information.

Solution 7 - Java

I would recommend creating a HashMap from set A, and then iterating through set B and checking if any element of B is in A. This would run in O(|A|+|B|) time (as there would be no collisions), whereas retainAll(Collection<?> c) must run in O(|A|*|B|) time.

Solution 8 - Java

There's a bit rough method to do that. If and only if the A set contains some B's element than the call

A.removeAll(B)

will modify the A set. In this situation removeAll will return true (As stated at removeAll docs). But probably you don't want to modify the A set so you may think to act on a copy, like this way:

new HashSet(A).removeAll(B)

and the returning value will be true if the sets are not distinct, that is they have non-empty intersection.

Also see Apache Commons Collections

Solution 9 - Java

You can use retainAll method and get the intersection of your two sets.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRahul gargView Question on Stackoverflow
Solution 1 - JavaFrontlineView Answer on Stackoverflow
Solution 2 - JavagplView Answer on Stackoverflow
Solution 3 - JavaYamchaView Answer on Stackoverflow
Solution 4 - JavaCaTalyst.XView Answer on Stackoverflow
Solution 5 - JavaAdam111pView Answer on Stackoverflow
Solution 6 - JavaSuresh KumarView Answer on Stackoverflow
Solution 7 - JavaZéychinView Answer on Stackoverflow
Solution 8 - JavaPlapView Answer on Stackoverflow
Solution 9 - JavaArtemView Answer on Stackoverflow