I often need the maximum element of a collection according to the maximization of a criterion which produces a double or int value. Streams have the max() function which requires me to implement a comparator, which I find cumbersome. Is there a more concise syntax, such as names.stream().argmax(String::length)
in the following example?
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
public class ArgMax
{
public static void main(String[] args)
{
List<String> names = Arrays.asList("John","Joe","Marilyn");
String longestName = names.stream().max((String s,String t)->(Integer.compare(s.length(),t.length()))).get();
System.out.println(longestName);
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…