在Java中使用Lambda表达式查找Max
这是我的代码
List<Integer> ints = Stream.of(1,2,4,3,5).collect(Collectors.toList());
Integer maxInt = ints.stream()
.max(Comparator.comparing(i -> i))
.get();
System.out.println("Maximum number in the set is " + maxInt);
输出:
Maximum number in the set is 5
我无法在两个我在我的代码的下一部分
I cannot make distingues between two i
in below section of my code
Comparator.comparing(i -> i)
任何人都可以善待并解释两个之间的区别我
?
can anyone be kind and explain the difference between two i
?
方法 Comparator.comparing(...)
旨在创建一个 Comparator
,它使用基于对象属性的订单进行比较。使用lambda表达式时 i - >我
,这是(int i)的简写 - > {return i; }
这里,作为属性提供程序函数,生成的 Comparator
将比较值本身。当要比较的对象具有自然顺序为整数
具有此功能时,此方法有效。
The method Comparator.comparing(…)
is intended to create a Comparator
which uses an order based on a property of the objects to compare. When using the lambda expression i -> i
, which is a short writing for (int i) -> { return i; }
here, as a property provider function, the resulting Comparator
will compare the values itself. This works when the objects to compare have a natural order as Integer
has.
所以
Stream.of(1,2,4,3,5).max(Comparator.comparing(i -> i))
.ifPresent(maxInt->System.out.println("Maximum number in the set is " + maxInt));
与
Stream.of(1,2,4,3,5).max(Comparator.naturalOrder())
.ifPresent(maxInt->System.out.println("Maximum number in the set is " + maxInt));
虽然后者效率更高,因为它对所有具有自然顺序的类型实施为单身(并实现 Comparable
)。
though the latter is more efficient as it is implemented as singleton for all types which have a natural order (and implement Comparable
).
max
的原因>需要一个 Comparator
,因为你使用的泛型类 Stream
可能包含任意对象。
The reason why max
requires a Comparator
at all, is because you are using the generic class Stream
which might contain arbitrary objects.
这允许,例如使用它像 streamOfPoints.max(Comparator.comparing(p-> px))
来查找最大 x $ c的点$ c>值而
点
本身没有自然顺序。或者做类似 streamOfPersons.sorted(Comparator.comparing(Person :: getAge))
。
This allows, e.g. to use it like streamOfPoints.max(Comparator.comparing(p->p.x))
to find the point with the largest x
value while Point
itself does not have a natural order. Or do something like streamOfPersons.sorted(Comparator.comparing(Person::getAge))
.
使用时专门的 IntStream
您可以直接使用自然订单,这可能更有效:
When using the specialized IntStream
you can use the natural order directly which is likely to be more efficient:
IntStream.of(1,2,4,3,5).max()
.ifPresent(maxInt->System.out.println("Maximum number in the set is " + maxInt));
说明自然顺序与基于房产的订单:
To illustrate the difference between "natural order" and a property based order:
Stream.of("a","bb","aaa","z","b").max(Comparator.naturalOrder())
.ifPresent(max->System.out.println("Maximum string in the set is " + max));
这将打印
集合中的最大字符串是z
Maximum string in the set is z
作为字符串的自然顺序
s是字典顺序,其中 z
大于 b
,大于 a
as the natural order of String
s is the lexicographical order where z
is greater than b
which is greater than a
另一方面
Stream.of("a","bb","aaa","z","b").max(Comparator.comparing(s->s.length()))
.ifPresent(max->System.out.println("Maximum string in the set is " + max));
将打印
集合中的最大字符串是aaa
Maximum string in the set is aaa
as aaa
具有流中所有字符串
的最大长度。这是 Comparator.comparing
的预期用例,在使用方法引用时可以使其更具可读性,即 Comparator.comparing(String :: length )
这几乎说不出话来......
as aaa
has the maximum length of all String
s in the stream. This is the intended use case for Comparator.comparing
which can be made even more readable when using method references, i.e. Comparator.comparing(String::length)
which almost speaks for itself…