Java 8中功能接口的定义

参见英文答案 > Precise definition of “functional interface” in Java 8                                    6个
Java 8中功能接口的定义说:

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.


List<Double> temperature = 
   new ArrayList<Double>(Arrays.asList(new Double[] { 20.0, 22.0, 22.5 }));
temperature.sort((a, b) -> a > b ? -1 : 1);


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.set((E) e);


Lambda Expression should be assignable to a Functional Interface




If an interface declares an abstract method overriding one of the public methods of java.lang.Object, that also does not count toward the interface’s abstract method count since any implementation of the interface will have an implementation from java.lang.Object or elsewhere.

因此,这使得Comparator接口成为功能方法为compare(o1, o2)的功能接口.

λ表达式(a,b) – > a> b? -1:1符合该合同:它声明2个参数并返回一个int.


转载注明原文:Java 8中功能接口的定义