We have actually used functional programming in the preceding chapters. In Chapter 6, Data Structures, Generics and Popular Utilities, we talked about the Iterable interface and its default void forEach (Consumer<T> function) method and provided the following example:
Iterable<String> list = List.of("s1", "s2", "s3");
System.out.println(list); //prints: [s1, s2, s3]
list.forEach(e -> System.out.print(e + " ")); //prints: s1 s2 s3
You can see how a Consumer e -> System.out.print(e + " ") function is passed into the forEach() method and applied to each element flowing into this method from the list. We will discuss the Consumer function shortly.
We also mentioned two methods of the Collection interface that accept a function as a parameter too:
- The default...