Function.Identity() in Java Stream with Examples

In this post, I will be sharing what is Function.identity() package, what is Function.identity() method, and  syntax along with various examples of Function.identity() using Java Stream.

Read Also: Java Stream Interview Questions and Answers

What is the Function.identity() package in Java?

java.util.function.Function is the package we have to use for Function.identity() in Java.

What is the Function.identity() method in Java?

According to Oracle docs, identity is a static method of Function interface that returns a Function that always returns its input argument.

Function.identity() method in Java with examples
What is the syntax of the Function.identity() method in Java?


static <T> Function<T,T> identity()


Can we use Function.identity() with primitive types in Java?

No, we can not use Function.identity() with primitive types in Java as it requires reference types.

Examples of Function.identity() method in Java

Example 1



import java.util.function.Function;

public class IdentityFunctionExample {
    public static void main(String args[]) 
    {
        // Using String as Input for Function.identity()
        Function<String,String> func = Function.identity();
        System.out.println(func.apply("Alive is Awesome"));
        
        // Using Integer as input for Function.identity()
        Function<Integer,Integer> func2 = Function.identity();
        System.out.println(func2.apply(8));
        
        // Using lambda expression and String as input
        Function<String,String> stringFunction = t -> t;
        System.out.println(stringFunction.apply("Be in present"));
        
        // Using lambda expression and Integer as input
        Function<Integer,Integer> integerFunction = t -> t;
        System.out.println(integerFunction.apply(4));    
    }
}


Output:
Alive is Awesome
8
Be in present
4


Example 2



import java.util.function.Function;
import java.util.List;
import java.util.Arrays;

public class IdentityFunctionExample2 {
    public static void main(String args[]) 
    {
        // Creating List<String>
        List<String> list = Arrays.asList("Alive is Awesome", "Be in Present", "Love Yourself");
        // Using List<String> 
        System.out.println(" Function.identity() output: ");
        list.stream().map(Function.identity()).forEach(System.out::println);
        System.out.println(" t -> t lambda expression output: ");
        list.stream().map(t -> t).forEach(System.out::println);
    }
}


Output:
Function.identity() output:
Alive is Awesome
Be in Present
Love Yourself
t -> t lambda expression output:
Alive is Awesome
Be in Present
Love Yourself

Is t -> t same as Function.identity() method in Java?

Let's find out whether t -> t same as Function.identity() method in Java:

import java.util.function.Function;

public class IdentityFunctionExample3 {
    public static void main(String args[]) 
    {
        Function<Integer, Integer> func1 = Function.identity();
        Function<Integer, Integer> func2 = Function.identity();
        Function<Integer, Integer> func3 = Function.identity();
        
        Function<Integer, Integer> intFunc1 = t -> t;
        Function<Integer, Integer> intFunc2 = t -> t;
        Function<Integer, Integer> intFunc3 = t -> t;
        
        System.out.println(func1);
        System.out.println(func2);
        System.out.println(func3);
        
        System.out.println(intFunc1);
        System.out.println(intFunc2);
        System.out.println(intFunc3);
    }
}


Output:
java.util.function.Function$$Lambda$1/0x0000000800034d78@7b1d7fff
java.util.function.Function$$Lambda$1/0x0000000800034d78@7b1d7fff
java.util.function.Function$$Lambda$1/0x0000000800034d78@7b1d7fff
IdentityFunctionExample3$$Lambda$2/0x0000000800000a08@2e5c649
IdentityFunctionExample3$$Lambda$3/0x0000000800000c48@136432db
IdentityFunctionExample3$$Lambda$4/0x0000000800001000@7382f612


From the above output, we can conclude that Function.identity() method will always return the same instance whereas each occurrence of (t -> t) or identifier -> identifier will not only create its own instance but even have a distinct implementation class.

That's all for today. Please mention in the comments if you have any questions related to Function.identity() method in Java Stream with examples.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry