Java 8找到最大值 [英] Java 8 find max
本文介绍了Java 8找到最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用 max()
来查找列表中的最大值,但下面的代码返回 4
虽然最大值为 90
。
I am using max()
to find the maximum value in the list but the code below returns 4
though the max value is 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());
推荐答案
流#MAX(比较)
需要比较器
。您将要使用 Integer#compare(int,int)
作为该比较函数。
Stream#max(Comparator)
takes a Comparator
. You'll want to use Integer#compare(int, int)
as that comparison function.
list.stream().max(Integer::compare).get()
你提供 Integer#max(int,int)
作为的实现比较器#comparison(int,int)
。该方法与 Comparator#compare
的要求不符。它不返回指示哪个值最大的值,而是返回最大值。
You were providing Integer#max(int, int)
as an implementation of Comparator#compare(int, int)
. That method doesn't match the requirements of Comparator#compare
. Instead of returning a value indicating which is biggest, it returns the value of the biggest.
这篇关于Java 8找到最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文