Given a list of integer elements, how to get the max value and it\'s index in one shot. If there is more than one element with same max value, returning index of any one of
If you don't mind using third-party code, my StreamEx library provides some shortcuts for this task:
List intArr = Arrays.asList(5, 8, 3, 2);
IntStreamEx.ofIndices(intArr)
.maxBy(intArr::get)
.ifPresent(ix->System.out.println("Index "+ix+", value "+intArr.get(ix)));
Internally it's close to the first solution provided by @Holger (no boxing).