Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking whether scala set intersection is empty

Is there a recommended way to check whether two scala collection Sets have a non-empty intersection? There is of course the obvious

set1.intersect(set2).isEmpty

But that actually constructs the intersection set first, unless I am mistaken. Is there a better/faster way?

like image 697
mitchus Avatar asked Jun 01 '26 08:06

mitchus


1 Answers

The idiomatic way is to use .intersect() or .diff() but both ways build new collection internally.

Fastest ways are:

  • Manually iterate over first set until match in the second
  • Use probabilistic Bloom Filter that take some time to construct it but can compare two sets (even very huge) very fast (occasionally may give false positives)
like image 175
Artur Rashitov Avatar answered Jun 04 '26 11:06

Artur Rashitov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!