As of the current JRE implementation, Function.identity() will always return the same instance while each occurrence of identifier -> identifier will not only create its own instance but even have a distinct implementation class. For more details, see here.

The reason is that the compiler generates a synthetic method holding the trivial body of that lambda expression (in the case of x->x, equivalent to return identifier;) and tell the runtime to create an implementation of the functional interface calling this method. So the runtime sees only different target methods and the current implementation does not analyze the methods to find out whether certain methods are equivalent.

So using Function.identity() instead of x -> x might save some memory but that shouldn’t drive your decision if you really think that x -> x is more readable than Function.identity().

You may also consider that when compiling with debug information enabled, the synthetic method will have a line debug attribute pointing to the source code line(s) holding the lambda expression, therefore you have a chance of finding the source of a particular Function instance while debugging. In contrast, when encountering the instance returned by Function.identity() during debugging an operation, you won’t know who has called that method and passed the instance to the operation.

Answer from Holger on Stack Overflow
🌐
How to do in Java
howtodoinjava.com › home › java streams › java stream to map with collectors.tomap() examples
Java Stream to Map With Collectors.toMap() Examples
March 18, 2019 - The Collectors.toMap() method collects stream items into a Map with a key mapper and a value mapper, and Function.identity() keeps the item itself as the key or the value.
🌐
Medium
medium.com › @barbieri.santiago › groupingby-and-function-identity-in-java-streams-202be061b49c
Function.identity in Java Streams | by Santiago | Medium
December 25, 2024 - It is essentially an identity function that satisfies the property f(x) = x. ... import java.util.List; import java.util.Map; import java.util.function.Function; import java.util.stream.Collectors; public class FunctionIdentityExample { public static void main(String[] args) { List<String> names = List.of("Maria", "Juan", "Jesus"); // Create a map where both the key and value are the same Map<String, String> nameMap = names.stream() .collect(Collectors.toMap(Function.identity(), Function.identity())); System.out.println(nameMap); // {Jesus=Jesus, Juan=Juan, Maria=Maria} } }
Top answer
1 of 4
472

As of the current JRE implementation, Function.identity() will always return the same instance while each occurrence of identifier -> identifier will not only create its own instance but even have a distinct implementation class. For more details, see here.

The reason is that the compiler generates a synthetic method holding the trivial body of that lambda expression (in the case of x->x, equivalent to return identifier;) and tell the runtime to create an implementation of the functional interface calling this method. So the runtime sees only different target methods and the current implementation does not analyze the methods to find out whether certain methods are equivalent.

So using Function.identity() instead of x -> x might save some memory but that shouldn’t drive your decision if you really think that x -> x is more readable than Function.identity().

You may also consider that when compiling with debug information enabled, the synthetic method will have a line debug attribute pointing to the source code line(s) holding the lambda expression, therefore you have a chance of finding the source of a particular Function instance while debugging. In contrast, when encountering the instance returned by Function.identity() during debugging an operation, you won’t know who has called that method and passed the instance to the operation.

2 of 4
126

In your example there is no big difference between str -> str and Function.identity() since internally it is simply t->t.

But sometimes we can't use Function.identity because we can't use a Function. Take a look here:

List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);

this will compile fine

int[] arrayOK = list.stream().mapToInt(i -> i).toArray();

but if you try to compile

int[] arrayProblem = list.stream().mapToInt(Function.identity()).toArray();

you will get compilation error since mapToInt expects ToIntFunction, which is not related to Function. Also ToIntFunction doesn't have identity() method.

🌐
Stack Abuse
stackabuse.com › guide-to-java-8-collectors-tomap
Guide to Java 8 Collectors: toMap()
March 30, 2023 - This method simply returns a function ... each object (the object itself) to their names easily: Map<String, Student> nameToStudentObject = students.stream() .collect(Collectors.toMap(Student::getName, Function.identity()));...
🌐
Baeldung
baeldung.com › home › java › java collections › java map › java collectors tomap
Java Collectors toMap | Baeldung
February 26, 2026 - To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams: ... In this quick tutorial, we’re going to talk about the toMap() method of the Collectors class.
🌐
amitph
amitph.com › home › java › examples of converting list to map using streams
Examples of Converting List to Map using Streams | amitph
November 22, 2024 - Map<Long, User> map = users.stream() .collect (Collectors.toMap(User::getId, Function.identity()));Code language: Java (java) Now, we get a Map of the user Id to the user instance.
🌐
Blogger
javahungry.blogspot.com › 2023 › 04 › function-identity-examples.html
Function.Identity() in Java Stream with Examples | Java Hungry
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 ·
🌐
Medium
medium.com › @ilakk2023 › understanding-function-identity-f237d584b487
31. Understanding Function.identity() | by Ilakkuvaselvi (Ilak) Manoharan | Medium
April 30, 2024 - When applied to the input "Hello, World!", it returns the same string. import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class StreamIdentityExample { public static void main(String[] args) { List<String> words = Arrays.asList("apple", "banana", "orange"); // Use identity() in a stream operation List<String> result = words.stream() .map(Function.identity()) .collect(Collectors.toList()); System.out.println(result); // Output: [apple, banana, orange] } }
Find elsewhere
🌐
Delft Stack
delftstack.com › home › howto › java › java stream to map
How to Convert Stream Element to Map in Java | Delft Stack
February 2, 2024 - ... ```java List<Double> L1 = ... in each. To add more to our primary mapping method: 2.1 It takes a key and value mapper to generate a Map from Stream objects....
🌐
ZetCode
zetcode.com › java › stream-identity-function
Identity Function in Java Streams
Here we use Function.identity as the mapping function, which results in no change to the stream elements.
🌐
Baeldung
baeldung.com › home › java › java collections › java map › how to convert list to map in java
How to Convert List to Map in Java | Baeldung
April 4, 2025 - Starting with Java 8, we can convert ... public Map<Integer, Animal> convertListAfterJava8(List<Animal> list) { Map<Integer, Animal> map = list.stream() .collect(Collectors.toMap(Animal::getId, Function.identity())); return map; }...
🌐
CodingNConcepts
codingnconcepts.com › java › streams-with-map-java-8
Using Streams API with Map in Java 8 - Coding N Concepts
September 30, 2023 - Notice the use of Function.identity() method to collect the object itself. In previous examples, we used the id as a key which perfectly works because the key of a Map should be unique.
🌐
Medium
medium.com › geekculture › java-streams-collect-a-stream-to-map-40480b96a38
Java Streams: Collect a Stream to Map | Geek Culture
May 7, 2022 - How to Collect a stream back into a list using Collectors . toMap method. Examples with lambda functions, method reference, identity functions an functional programming.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › stream › Stream.html
Stream (Java Platform SE 8 )
July 21, 2026 - T result = identity; for (T element : this stream) result = accumulator.apply(result, element) return result; but is not constrained to execute sequentially.
🌐
Baeldung
baeldung.com › home › java › java collections › java map › convert a stream into a map or multimap in java
Convert a Stream into a Map or Multimap in Java | Baeldung
December 26, 2023 - The above test method starts by creating a Stream of strings stringStream, which is put into a Map using Collectors.toMap(). This function takes each string as its key and value, separated by commas, to consolidate multiple entries for the same key. We may also use the Stream.reduce() operator. This method can help us construct the values of the stream into a Map using an identity and an accumulation function.
🌐
YouTube
youtube.com › shane crouch
Java List Stream to Map using the Collectors toMap API : Java ...
In this Java Streams Tutorial, we take a Java List stream it to map using the collectors toMap API. We should you how to stream a list to hashmap and other m...
Published: June 30, 2020
Views: 278
🌐
Java Guides
javaguides.net › 2023 › 09 › java-function-identity.html
Java Function identity()
September 27, 2023 - The Function.identity() method is a static utility method that returns a function that always returns its input argument. In simpler terms, it's a function where the output is always the same as the input.
🌐
Marc Nuri
blog.marcnuri.com › home › java streams: convert list into map
Java Streams: Convert List into Map - Marc Nuri
January 27, 2026 - ListToMapOrdered.java · LinkedHashMap<String, Repository> map = repos.stream().collect(Collectors.toMap( Repository::name, Function.identity(), (existing, replacement) -> existing, LinkedHashMap::new )); The fourth parameter is a Supplier that provides the Map implementation.
🌐
Java67
java67.com › 2019 › 05 › how-to-convert-stream-to-list-set-map-in-java.html
How to Convert a Stream to List, Set, and Map in Java? Example Tutorial | Java67
Our complete Java program demonstrates how you can use the Collectors class to convert a Stream of values into List, Set, Map, and ConcurrentMap in Java. Not only you will learn to convert Stream to generic Collection classes but also specific ones like ArrayList, LinkedList, HashMap, HashSet, LinkedHashMap, and TreeMap.