How Is A Comparator Interface A Functional Interface?

Getting your Trinity Audio player ready...

The Comparator interface is useful for defining sorting in a user-defined class. It is a functional interface, even though the Comparator interface contains two abstract classes.

How?

Let’s check the Comparator class from the java.util package.

package java.util;

@FunctionalInterface
public interface Comparator<T> {

  // abstract method #1
  int compare(T o1, T o2);

  // abstract method #2
  boolean equals(Object obj);

  // other static and default methods
  ...


}

Now, you’ll notice that there are two abstract methods. Ideally, an interface can only be called a functional interface if it has only one abstract method.

However, in this case, the abstract method, boolean equals(Object obj), is one of the public methods of the Object class from the java.lang package. This abstract method is not counted since any implementation of the interface will have an implementation from the Object class.

That means, Comparator only has one abstract method int compare(T o1, T o2), and hence, it meets the definition of functional interface.