Java 8 流 max() 函数参数类型 Comparator vs Comparable函数、参数、类型、max

2023-09-07 09:55:06 作者:浅挚绊离兮

我写了一些简单的代码,如下所示.这个类工作正常,没有任何错误.

I wrote some simple code like below. This class works fine without any errors.

public class Test {
    public static void main(String[] args) {
        List<Integer> intList = IntStream.of(1,2,3,4,5,6,7,8,9,10).boxed().collect(Collectors.toList());
        int value = intList.stream().max(Integer::compareTo).get();

        //int value = intList.stream().max(<Comparator<? super T> comparator type should pass here>).get();

        System.out.println("value :"+value);
    }
}

正如代码注释所示,max() 方法应该传递 Comparator