Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are practical uses of the java.util.function.Function.identity method?

Tags:

java

java-8

Why should I use Function.identity() when it returns the same thing which it receives without doing anything using the input or modifying the input in some way?

Apple apple = new Apple(10, "green"); Function<Apple, Apple> identity = Function.identity(); identity.apply(apple); 

There must be some practical usage of this which I am not able to figure out.

like image 949
arunveersingh Avatar asked Sep 27 '18 08:09

arunveersingh


People also ask

What is the use of function identity in Java?

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.

What is the purpose of an identity function?

In mathematics, an identity function, also called an identity relation, identity map or identity transformation, is a function that always returns the value that was used as its argument, unchanged. That is, when f is the identity function, the equality f(X) = X is true for all values of X to which f can be applied.

What is the use of function in Java 8?

In Java 8, Function is a functional interface; it takes an argument (object of type T) and returns an object (object of type R). The argument and output can be a different type.


2 Answers

The intended usage is when you're using a method that accepts a Function to map something, and you need to map the input directly to the output of the function (the 'identity' function).

As a very simple example, mapping a list of persons to a map from name to person:

import static java.util.function.Function.identity  // [...]  List<Person> persons = ... Map<String, Person> = persons.stream()         .collect(Collectors.toMap(Person::name, identity())) 

The identity() function is just for convenience and readability. As Peter indicates in his answer, you could just use t -> t, but personally I think that using identity() communicates intent better as it leaves no room for interpretation like wondering if the original author forgot to do a transformation in that lambda. I admit though that is highly subjective, and assumes the reader knows what identity() does.

Possibly it may have some additional advantages in terms of memory as it reuses a single lambda definition, instead of having a specific lambda definition for this call. I think that impact is probably negligible in most cases.

like image 166
Mark Rotteveel Avatar answered Sep 28 '22 07:09

Mark Rotteveel


You can use it for a frequency count for example.

public static <T> Map<T, Long> frequencyCount(Collection<T> words) {     return words.stream()             .collect(Collectors.groupingBy(Function.identity(),                     Collectors.counting()); } 

In this case, you are saying the key to group by is the element in the collection (without transforming it).

Personally, I find this briefer

import static java.util.stream.Collectors.*;  public static Map<String, Long> frequencyCount(Collection<String> words) {     return words.stream()             .collect(groupingBy(t -> t,                     counting()); } 
like image 31
Peter Lawrey Avatar answered Sep 28 '22 08:09

Peter Lawrey