The definition of a functional Interface in Java 8 says:
A functional interface is defined as any interface that has exactly one explicitly declared abstract method. (The qualification is necessary because an interface may have non-abstract default methods.) This is why functional interfaces used to be called Single Abstract Method (SAM) interfaces, a term that is still sometimes seen.
So how come we have this:
List<Double> temperature =
new ArrayList<Double>(Arrays.asList(new Double[] { 20.0, 22.0, 22.5 }));
temperature.sort((a, b) -> a > b ? -1 : 1);
As the sort
method in List
is:
default void sort(Comparator<? super E> c) {
Object[] a = this.toArray();
Arrays.sort(a, (Comparator) c);
ListIterator<E> i = this.listIterator();
for (Object e : a) {
i.next();
i.set((E) e);
}
}
And the lambda expression says:
Lambda Expression should be assignable to a Functional Interface
The Comparator
interface has two abstract methods which are compare
and equals
and is annotated with @FunctionalInterface
. Does not this violate the definition of a functional interface having only one abstract method?