Java 8 identity function Function.identity() returns a Function
that always returns it’s input argument. In this article we will see various examples using Function.identity().
The identity function in math is one in which the output of the function is equal to its input. In Java, Function is a functional interface whose identity
method returns a Function
that always returns its input arguments.
Following code is implementation of identity
method in Function interface.
static <T> Function<T, T> identity() { return t -> t; }
Function<T, T> : First parameter is type of input of function and second parameter is type of output, identity function always returns its input arguments.
1. Java 8 identity function Example 1
The following example shows you how to create a Function
using identity
method, and using lambda.
public class Java8FunctionIdentityExample1 { public static void main(String[] args) { Function<Integer, Integer> identityFunction = Function.identity(); Function<Integer, Integer> intFunction = e -> e; // using lambda expression System.out.println(identityFunction.apply(10)); // 10 System.out.println(intFunction.apply(10)); // 10 List<String> names = Arrays.asList( "Peter", "Martin", "John", "Vijay", "Arthur" ); // Just for example System.out.println("----- Function.identity() -----"); names.stream().map(Function.identity()).forEach(System.out::println); System.out.println("----- Function(e-> e) -----"); names.stream().map(e->e).forEach(System.out::println); } }
Output :
10 10 ----- Function.identity() ----- Peter Martin John Vijay Arthur ----- Function(e-> e) ----- Peter Martin John Vijay Arthur
2. Function.identity() Example 2
In Example 1 we can notice Function.identity()
and e -> e
has given same results. Are they same? well, let’s have a look into another following example.
public class Java8FunctionIdentityExample2 { public static void main(String[] args) { Function<Integer, Integer> identityFunction1 = Function.identity(); Function<Integer, Integer> identityFunction2 = Function.identity(); Function<Integer, Integer> identityFunction3 = Function.identity(); Function<Integer, Integer> intFunction1 = e -> e; Function<Integer, Integer> intFunction2 = e -> e; Function<Integer, Integer> intFunction3 = e -> e; System.out.println(identityFunction1); System.out.println(identityFunction2); System.out.println(identityFunction3); System.out.println(intFunction1); System.out.println(intFunction2); System.out.println(intFunction3); } }
Output :
java.util.function.Function$$Lambda$23/[email protected] java.util.function.Function$$Lambda$23/[email protected] java.util.function.Function$$Lambda$23/[email protected] com.javabydeveloper.core8.function.Java8FunctionIdentityExample2$$Lambda$24/[email protected] com.javabydeveloper.core8.function.Java8FunctionIdentityExample2$$Lambda$25/[email protected] com.javabydeveloper.core8.function.Java8FunctionIdentityExample2$$Lambda$26/[email protected]
In the test results we can notice Function.identity()
return same lambda instance with in the program, and e -> e
lambda function return a different instance each time.
3. Function.identity() Example 3
Following example shows you, how to convert list to set using Stream, each item in set contains the duplicate count if the list has any duplicate item. If the list don’t have duplicate items Stream.map()
uses Function.identity()
otherwise it uses lambda function.
public class Java8FunctionIdentityExample3 { // Convert list to set, and if list has duplicates -> show duplicate count public static void main(String[] args) { List<String> names = Arrays.asList( "Peter", "Martin", "John", // "Peter", "Vijay", // "Martin", // "Peter", "Arthur" ); Set<String> namesSet = new HashSet(names); // names.size() != namesSet.size();// => true if duplicates names.stream() .map(getFunction(names, names.size() != namesSet.size())) .collect(Collectors.toSet()) .forEach(System.out::println); } static Function<String, String> getFunction(List<String> names, boolean hasDuplicates){ // Collections.frequency(names, name) => to get duplicate count return hasDuplicates ? name -> name+" ("+Collections.frequency(names, name)+")" : Function.identity(); } }
Output 1 :
Vijay Arthur John Martin Peter
Remove comments for the list items and run again the program, you will see following results.
Output 2 :
Peter (3) Martin (2) John (1) Vijay (1) Arthur (1)
4. Conclusion
In this article we have seen various examples identity()
method of Function interface to understand it’s usage. For more examples of Function interface methods refer to the article Java 8 Function interface.
Very nice examples Satish!
Thanks Rafael for your feedback!