本文介绍了FunctionalInterface Comparator有2个抽象方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
学习Java 8 Lambdas,只是想知道编译器如何知道Comparator中哪个方法用于lambda表达式?
它似乎不是SAM界面?它有2种抽象方法:
Learning Java 8 Lambdas and just wondering how the compiler knows which method in Comparator to use for the lambda expression? It doesn't seem to be a SAM interface? It has 2 abstract methods:
@FunctionalInterface
public interface Comparator<T> {
int compare(T o1, T o2);
boolean equals(Object obj);
}
推荐答案
equals()
不是抽象方法。此方法重写 Object.equals(Object)
,并且只有Comparator接口才能将javadoc附加到该方法,解释比较器应如何实现 equals()
。
equals()
is not an abstract method. This method overrides Object.equals(Object)
, and is there only for the Comparator interface to be able to have javadoc attached to the method, explaining how comparators should implement equals()
.
参见:
这篇关于FunctionalInterface Comparator有2个抽象方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!