In Java8, having a List<Item> list I process it sequentially as below:
ConcurrentMap<String, Integer> map = new ConcurrentHashMap<String, Integer>();
for (int i1 = 0; i1 < list.size() - 1; i1++) {
Item item1 = list.get(i1);
for (int i2 = i1 + 1; i2 < list.size(); i2++) {
Item item2 = list.get(i2);
doSomething(item1, item2);
}
}
So I process all ordered pairs of items from the list (index of item1 < index of item2). Now, I would like to run doSomething(item1, item2) function in parallel for each ordered pair. What would be the best strategy to achieve that? Interested in the fastest possible code. Java8 streams are welcome.
doSomething for instance does: map.put(item1.key + " " + item2.key, item1.val + item2.val);.
The number of ordered pairs is n * (n - 1) / 2 where n is the size of the list. I also consider to split the amount of job evenly to reach the load-balance (at the moment assume each pair's execution time is the same). So it is not required to call doSomething(item1, item2) function in parallel for each ordered pair, but possibly for a set of prepared pairs.