Say I have a list with elements (34, 11, 98, 56, 43)
.
Using Java 8 streams, how do I find the index of the minimum element of the list (e.g. 1 in this case)
Here's two possible solutions using my StreamEx library:
int idx = IntStreamEx.ofIndices(list).minBy(list::get).getAsInt();
Or:
int idx = EntryStream.of(list).minBy(Entry::getValue).get().getKey();
The second solution internally is very close to one proposed by @AlexisC. The first one is probably the fastest as it does not use boxing (internally it's a reduce operation).
Without using third-party code @Misha's answer looks the best for me.