yohhoyの日記

技術的メモをしていきたい日記

java.util.Comparatorは関数型インタフェース

Java 8のjava.util.Comparatorインタフェースは、その宣言からは2つのメソッドを持つようにみえるが関数型インタフェース(functional interface)である。
Comparator#equalsObject#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 Object), 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.

Chapter 9. Interfaces, 9.8. Functional Interfaces