Java 8のjava.util.Comparatorインタフェースは、その宣言からは2つのメソッドを持つようにみえるが関数型インタフェース(functional interface)である。
Comparator#equals
はObject#equals
メソッドのオーバーライドとして扱われ、Comparator#compare
メソッドのみが唯一の抽象メソッドとみなされる。
@FunctionalInterface public interface Comparator<T> { int compare(T o1, T o2); boolean equals(Object obj); // staticメソッド群... // defaultメソッド群... }
Java Language Specification, Java SE 8 Editionより一部引用(下線部は強調)。
A functional interface is an interface that has just one abstract method (aside from the methods of
Chapter 9. Interfaces, 9.8. Functional InterfacesObject
), and thus represents a single function contract. This "single" method may take the form of multiple abstract methods with override-equivalent signatures inherited from superinterfaces; in this case, the inherited methods logically represent a single method.