Java 8 stream#39;s .min() and .max(): why does this compile?(Java 8 流的 .min() 和 .max():为什么会编译?)
问题描述
Note: this question originates from a dead link which was a previous SO question, but here goes...
See this code (note: I do know that this code won't "work" and that Integer::compare
should be used -- I just extracted it from the linked question):
final ArrayList <Integer> list
= IntStream.rangeClosed(1, 20).boxed().collect(Collectors.toList());
System.out.println(list.stream().max(Integer::max).get());
System.out.println(list.stream().min(Integer::min).get());
According to the javadoc of .min()
and .max()
, the argument of both should be a Comparator
. Yet here the method references are to static methods of the Integer
class.
So, why does this compile at all?
Let me explain what is happening here, because it isn't obvious!
First, Stream.max()
accepts an instance of Comparator
so that items in the stream can be compared against each other to find the minimum or maximum, in some optimal order that you don't need to worry too much about.
So the question is, of course, why is Integer::max
accepted? After all it's not a comparator!
The answer is in the way that the new lambda functionality works in Java 8. It relies on a concept which is informally known as "single abstract method" interfaces, or "SAM" interfaces. The idea is that any interface with one abstract method can be automatically implemented by any lambda - or method reference - whose method signature is a match for the one method on the interface. So examining the Comparator
interface (simple version):
public Comparator<T> {
T compare(T o1, T o2);
}
If a method is looking for a Comparator<Integer>
, then it's essentially looking for this signature:
int xxx(Integer o1, Integer o2);
I use "xxx" because the method name is not used for matching purposes.
Therefore, both Integer.min(int a, int b)
and Integer.max(int a, int b)
are close enough that autoboxing will allow this to appear as a Comparator<Integer>
in a method context.
这篇关于Java 8 流的 .min() 和 .max():为什么会编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java 8 流的 .min() 和 .max():为什么会编译?
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01