Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (3.5k points)
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).

1 Answer

0 votes
by (46k points)

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

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

Thus, the process returns false if the groups contain any basic elements.

Related questions

0 votes
2 answers
0 votes
2 answers
asked Nov 25, 2019 in Java by Anvi (10.2k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...