Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Java's Set's implement the UnionFind algorithm?

I know the Union Find algorithm from Wikipedia, and can find small implementations of them, but does Java itself use a similar algorithm for its Set's? If so I'd prefer to use Java Sets without recoding the wheel...

like image 547
propaganda Avatar asked Jan 12 '12 10:01

propaganda


2 Answers

Java's Set interface supports completely different operations (and is therefore appropriate for completely different use cases) that a Union Find structure.

like image 76
Michael Borgwardt Avatar answered Sep 21 '22 15:09

Michael Borgwardt


AFAIK No, to create a union of two sets, the common approach is to copy one set and add all the elements of the second. (Or copy all the element of both sets to the same set) e.g.

Set<E> set1, set2;

Set<E> union = new HashSet<E>(set1);
union.addAll(set2);
like image 29
Peter Lawrey Avatar answered Sep 24 '22 15:09

Peter Lawrey