I have been using Collections.max(list)
for many projects, but I occasionally want to find the index of that maximum element. I feel silly writing a function to do this for myself in every program I write.
Why does the Collections Interface not supply a Collections.maxIndex(list)
?
1
While there can be exactly one max value in a collection, there can be more than item representing that value. E.g {1, 9, 2, 9, 0}
has max value of 9
, represented by both elements [1]
and [3]
.
Note that not all collections support index access; e.g. a Set<Integer>
can have a meaningful maximum but accessing an element by index makes no sense in it.
Even if we limit the method to List
, it would be a bit hard to come up with one method to find indices of the maximum value that is not clumsy. You could return a list of indices, but then you’d lose the value, and in some collections, e.g. linked lists, accessing an element by index is slow. Since Java does not have an easy syntax for tuples, you’d have to return a special type of object with .getValue()
and .getIndices()
.
But I think that such an operation is just not common enough to be supported in the standard library. Finding a maximum is literally 3-4 lines of code, and tracking the index is another 1-2 lines, and there’s no much room to do it wrong. If you do it a lot, you can easily put it in your own utility class.
2
Please take a look at Java 8 Streams API.
Link: http://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html
(Disclaimer: I haven’t used it before.)
However, there’s no out-of-the-box implementation of maxIndex
. To enable that, someone will have to provide a high performance primitive pair or tuple stream implementation for Java 8 Streams.
The reason is this. Functionally speaking, maxIndex
is:
- Convert each value into a pair (tuple of 2) as:
(value, position)
- Reduce the stream as follows:
- Compare the two items,
(value1, position1)
with(value2, position2)
, - Keep the item with the higher value, and copy along its position.
- Namely,
return (item1.value > item2.value) ? item1 : item2;
- Namely,
- If a tie is possible, a tie-breaking mechanism is also needed, as pointed out in 9000’s answer. It will no longer be a reduction over the tuple, but will become a “collection of (value, position) tuples all having the same maximum value”.
- As long as a tie-breaking mechanism is implemented, the approach outlined here will still be applicable.
- Compare the two items,
- At the end of operation, return
item.value
as the maximum value, anditem.position
as the maximum index.- As pointed out by 9000’s answer, depending on the tie-breaking mechanism, there may be multiple results.
Unfortunately, as many Java advocates have pointed out, Java as a language is experiencing from an explosion of class-happiness, and this is due to the limitation of Java generics support. As a result, if you need anything not found in Java API, you have to roll your own, or find a library or toolbox that does.