I want to join the items of two lists respectively.
Here is my code :
1.
List<String> pairs = list1.stream()
.parallel()
.flatMap(item1 -> list2.stream()
.parallel()
.map(item2 -> item1 + " " + item2))
.collect(Collectors.toList());
When I try to similar methods, above method is fastest.. But I feel like this method is not parallel. (because the order of result is always same..!)
Is there any faster way? The order of the final result list does not matter.
Thanks!
=======================================
I tried 2 more methods
2.
List<String> pairs = new ArrayList<>();
for(String item1 : list1)
for(String item2 : list2)
pairs.add(item1 + " " + item2);
pool.submit(() -> {
List<String> pairs = list1.stream()
.parallel()
.flatMap(item1 -> list2.stream()
.parallel()
.map(item2 -> item1 + " " + item2))
.collect(Collectors.toList());
}).get();