I have currently this code:
AtomicInteger counter = new AtomicInteger(0);
return IntStream.range(0, costs.length)
.mapToObj(i -> new int[]{costs[i][0]-costs[i][1], i})
.sorted(Comparator.comparingInt(d -> d[0]))
.mapToInt(s ->
counter.getAndIncrement() < costs.length/2 ? costs[s[1]][0] : costs[s[1]][1]
)
.sum();
Where I compute diff of two elements of an array and then sort it and in the end I need to process two halves independently.
Is there any better way to do this than using AtomicInteger
as a counter? Is there some method like mapToIntWithIndex
that is accessible inside JDK (not in external libraries)? Is there something like zip()
in python where I could join indices together with stream? If not is there any plan to add this to next Java releases?