Single Abstract Method Interfaces (SAM Interfaces) are also known as Functional Interfaces.A functional interface in Java is an interface that contains only one abstract method. They can have any number of default or static methods.
The main roles of functional interfaces are:
Support for Lambda Expressions and Method References
Support for Stream API
Here’s an example of a functional interface which is implemented using a lambda expression:
🪧Note:
The @FuncitonalInterface annotation is optional.
The parentheses () are optional if there is only one parameter.
The curly braces {} are optional if there is only one statement.
Create SAM Interface Instances
Instances of functional interfaces can be created with lambda expressions, method references, or constructor references.
Examples of SAM Interfaces
Java’s standard library includes many commonly used SAM interfaces, such as Runnable, Callable, Comparable,Consumer, and many others in the java.util.function package.
Here’s an example of using the Comparator and Consumer interfaces with lambda expressions: