今天,我尝试学习Java 8中的一些特性,具体涉及Lambda表达式。我创建了一个新的比较器,如下所示:
当我阅读Comparator接口内的代码时,我感到困惑。虽然接口Comparator有两个方法compare()和equals(),但我们不需要实现它们全部。我已经找到了一些原因,为什么我们不需要实现方法equals()这里。但我也在javadocs中读到:
"If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile". 什么是接口? 所以,有人可以帮助我理解吗?不重写equals()仍然是合法的吗?
Comparator<String> strCom = new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return 0;
}
};
当我阅读Comparator接口内的代码时,我感到困惑。虽然接口Comparator有两个方法compare()和equals(),但我们不需要实现它们全部。我已经找到了一些原因,为什么我们不需要实现方法equals()这里。但我也在javadocs中读到:
"If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile". 什么是接口? 所以,有人可以帮助我理解吗?不重写equals()仍然是合法的吗?
equals
方法。 - SomeJavaGuyNote that it is always safe not to override Object.equals(Object). However, overriding this method may, in some cases, improve performance by allowing programs to determine that two distinct comparators impose the same order.
- biziclop