Lambda expressions usage
A lambda expression can be used in many different situations, including:
- Assigned to a variable
- Passed as a parameter
- Returned from a function or method
We will demonstrate how each of these is accomplished and then elaborate on the use of functional interfaces. As you may remember from Chapter 1, Getting Started with Functional Programming, a functional interface is an interface that has one and only one abstract method.
Consider the forEach
method supported by several classes and interfaces, including the List
interface. In the following example, a List
interface is created and the forEach
method is executed against it. The forEach
method expects an object that implements the Consumer
interface. This will display the three cartoon character names:
List<String> list = Arrays.asList("Huey", "Duey", "Luey"); list.forEach(/* Implementation of Consumer Interface*/);
More specifically, the forEach
method expects an object that implements...