我max()用来查找列表中的最大值,但下面的代码返回4虽然最大值是90.
List<Integer> list = new ArrayList<>(Arrays.asList(4,12,19,10,90,30,60,17,90));
System.out.println(list.stream().max(Integer::max).get());
Run Code Online (Sandbox Code Playgroud)
Sot*_*lis 11
Stream#max(Comparator)需要一个Comparator.您将要Integer#compare(int, int)用作该比较功能.
list.stream().max(Integer::compare).get()
Run Code Online (Sandbox Code Playgroud)
你Integer#max(int, int)作为一个实现提供Comparator#compare(int, int).该方法与要求不符Comparator#compare.它不返回指示哪个值最大的值,而是返回最大值.
| 归档时间: |
|
| 查看次数: |
4615 次 |
| 最近记录: |