June 2018
Beginner
722 pages
18h 47m
English
Functional interfaces of the java.util.function package have other helpful default methods. The one that stands out is the identity() method, which returns a function that always returns its input argument:
Function<Integer, Integer> id = Function.identity();System.out.println(id.apply(4)); //prints: 4
The identity() method is very helpful when some procedure requires providing a certain function, but you do not want the provided function to change anything. In such cases, you create an identity function with the necessary output type. For example, in one of our previous code snippets, we may decide that the multiplyByFive function should not change anything in the multiplyByFive.andThen(subtract7) chain: ...
Read now
Unlock full access