Saturday, 5 April 2025

function identity () or identity method

 

Function.identity() Overview :

It is a static method in the  interface. It returns a function that accepts an input and returns the same input without any modifications. Essentially, it is a "no-op" function.

The  method in Java 8 is part of the functional programming features introduced with the java.util.function package. It is a static method in the  interface and is often used in scenarios where you need a function that simply returns its input, without making any changes or transformations.

Definition:

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

Returns a function that always returns its input argument.

Type Parameters:

T - the type of the input and output objects to the function

Returns:

a function that always returns its input argument

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

This method returns a function that always returns its input argument.

Purpose:

  1. It is a convenience method used when you don't want to apply any transformation to the input.
  2. Often used in streams or other functional operations where a function is required but no transformation is needed.

Key Characteristics:

  • Useful as a default or placeholder function.
  • The returned function is a "no-op" (no operation).

Why should we use it?
  1. No Transformation Needed: It's useful when you need a function but don't want to apply any transformation to the input.
  2. Code Simplification: Instead of writing a lambda expression like , you can use .
  3. Default/Placeholder Function: Acts as a default mapper or identity function in various operations

Example1 :
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
        List<String> list = Arrays.asList("apple", "banana", "cherry");

        Map<String, String> map = list.stream()
            .collect(Collectors.toMap(Function.identity(), Function.identity()));

        System.out.println(map);
    }
}

Output:

{apple=apple, banana=banana, cherry=cherry}

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

import java.util.function.Function;

public class IdentityFunctionCompare {
    public static void main(String args[]) 
    {
        Function<Integer, Integer> funcIdentity = Function.identity();
        Function<Integer, Integer> funcIdentity1 = Function.identity();
        Function<Integer, Integer> funcIdentity2 = Function.identity();
        
        Function<Integer, Integer> intFuncMap1 = t -> t;
        Function<Integer, Integer> intFuncMap = t -> t;
        Function<Integer, Integer> intFuncMap = t -> t;
        
        System.out.println(funcIdentity);
        System.out.println(funcIdentity1);
        System.out.println(funcIdentity2);
        
        System.out.println(intFuncMap1);
        System.out.println(intFuncMap2);
        System.out.println(intFuncMap3);
    }
}


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