Java中的SAM接口是什么?

仅具有一种抽象方法的接口被称为功能接口,并且也被称为单一抽象方法接口(SAM接口)。一个抽象方法意味着允许使用默认方法或默认实现的抽象方法。SAM接口的实例是java.lang.Runnablejava.awt.event.ActionListen er,java.util.Comparatorjava.util.concurrent.CallableSAM接口可以使用来实现的λ表达式方法的引用

语法

@FunctionalInterface
public interface Changeable {
   public void change(T o);
}

示例

@FunctionalInterfaceinterface MyInterface {
   String reverse(String n);
}
public class LambdaReverseTest {
   public static void main( String[] args ) {
      MyInterface myInterface = (str) -> {     // Lambda Expression         String result = "";
         for(int i = str.length()-1; i >= 0 ; i--)
            result += str.charAt(i);
         return result;
      };
      System.out.println("The reverse of string is: " + myInterface.reverse("TutorialsPoint"));
   }
}

输出结果

The reverse of string is: tnioPslairotuT