iterator - Joining (union) of Sets inside a Set in Java -
i have map values sets of integers. i'd want in best way possible (using java api great) union of sets of integers.
map<long, set<integer>> map; what thought far loop through values() of map , manually add big set:
set<integer> bigset = new hashset<>(); iterator<set<integer>> iter = map.values().iterator(); while(iter.hasnext()) bigset.addall(iter.next()); also collection union backed map great. unfortunately stuck java 7.
on 1 hand use new java 8 fluent interface
import static java.util.stream.collectors.toset; set<integer> myunion = map .values() .stream() .flatmap(set -> set.stream()) .collect(toset()); on other hand suggest taking @ guava's setmultimap if can use external libraries.
Comments
Post a Comment