gpt4 book ai didi

java - Stream.max(Integer::max) :意外结果

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:01:26 26 4
gpt4 key购买 nike

<分区>

我正在学习 1z0-809 : Java SE 8 Programmer II使用 Enthuware 的模拟测试。

遇到这个问题。

List<Integer> ls = Arrays.asList(3,4,6,9,2,5,7);

System.out.println(ls.stream().reduce(Integer.MIN_VALUE, (a, b)->a>b?a:b)); //1
System.out.println(ls.stream().max(Integer::max).get()); //2
System.out.println(ls.stream().max(Integer::compare).get()); //3
System.out.println(ls.stream().max((a, b)->a>b?a:b)); //4

Which of the above statements will print 9?

答案是

1 and 3

但是还有别的东西。我不明白为什么

System.out.println(ls.stream().max(Integer::max).get()); // PRINTS 3

我尝试使用 peek 对其进行调试,但它无法帮助我理解。

我尝试使用 Integer::maxInteger::comparels 进行排序

ls.sort(Integer::max);     // [3, 4, 6, 9, 2, 5, 7]
ls.sort(Integer::compare); // [2, 3, 4, 5, 6, 7, 9]

当然,我知道 Integer::max 不是比较器,因此它具有相同的签名。对我来说,max 在第一种情况下应该是 7,因为它是最后一个元素,就像我用 Integer::compare

排序时一样>

有人能把它分解成简单的东西吗?

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com