I am trying to calculate the max sum pair of two lists with a faster way than what i've managed below, using "lightweight streams":
List<Float> pairsSum = new ArrayList<>();
// Get the list with all the sums
Stream.range(0, list1.size())
.forEach(i -> pairsSum.add(list1.get(i) + list2.get(i)));
// Get the index of the max pair
maxIndex = pairsSum.indexOf(Stream.of(pairsSum).max(Double::compare).orElse(0f));
The short solution is to use an
IntStream
and thereduce()
method:If you want the index, both values and the sum, you can use a custom
Result
class:Use it like this:
The time complexity in both cases is O(n).