I currently have a java program that uses nested for loops to compute the union and intersection of a list of set of integers. How to do this using java parallel streams ? The code i have currently is as follows
for(Set<Integer> x : listA) {
for (Set<Integer> y : listB) {
Set u = Sets.union(x,y); // Uses Guava library
Set i = Sets.intersection(x,y);
}
}
I would like to make this fast as listA and listB are large.
Intersection:
List<T> intersect = list1.stream()
.filter(list2::contains)
.collect(Collectors.toList());
Union:
List<T> union = Stream.concat(list1.stream(), list2.stream())
.distinct()
.collect(Collectors.toList());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With