Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

arg max in Java 8 streams?

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);
    }
}
like image 432
Konrad Höffner Avatar asked Dec 22 '14 16:12

Konrad Höffner


1 Answers

Use

String longestName = names.stream().max(Comparator.comparing(String::length)).get();

to compare elements on some property (can be more complex than that, but doesn't have to).

As Brian suggests in the comments, using Optional#get() like this is unsafe if there's a possibility that the Stream is empty. You'd be better suited to use one of the safer retrieval methods, such as Optional#orElse(Object) which will give you some default value if there is no max.

like image 55
Sotirios Delimanolis Avatar answered Sep 18 '22 21:09

Sotirios Delimanolis