🌐
Javabrahman
javabrahman.com › java-8 › java-8-java-util-function-function-tutorial-with-examples
Java 8 java.util.function.Function Tutorial with Examples
Java 8 code example showing usage ... java.util.function.Function; public class FunctionTRExample{ public static void main(String args[]){ Function<Employee, String> funcEmpToString= (Employee e)-> {return e.getName();}; List<Employee> employeeList= Arrays.asList(new Employee("Tom ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › function-interface-in-java
Function Interface in Java - GeeksforGeeks
July 11, 2025 - Explanation: In the above example we are using a Function interface to transform the Person object into a greeting string which contains the person's name. ... // Handling Multiple Conditions // with the help of Function import java.util.function.Function; public class Geeks { public static void main(String args[]) { Function<Integer, Integer> addFive = a -> a + 5; Function<Integer, Integer> multiplyByTwo = a -> a * 2; ...
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › function › package-summary.html
java.util.function (Java Platform SE 8 )
3 weeks ago - Functional interfaces often represent abstract concepts like functions, actions, or predicates. In documenting functional interfaces, or referring to variables typed as functional interfaces, it is common to refer directly to those abstract concepts, for example using "this function" instead of "the function represented by this object".
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › function › Function.html
Function (Java Platform SE 8 )
3 weeks ago - Returns a function that always returns its input argument. ... Java™ Platform Standard Ed. 8 ... Submit a bug or feature For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples...
🌐
Medium
geniusvipin.medium.com › use-of-java-util-function-package-dc5f131631f6
Use of java.util.function Package | by Dr. Vipin Kumar | Medium
February 9, 2021 - This interface has one function apply (), this function takes one input parameter as T and return value as R after performing some kind of operation on the input parameter. ... This T and R may have any type of value like Integer, Float, Double, ...
🌐
Baeldung
baeldung.com › home › java › core java › functional interfaces in java
Functional Interfaces in Java | Baeldung
March 26, 2025 - The article “Lambda Expressions and Functional Interfaces: Tips and Best Practices” describes in more detail the functional interfaces and best practices of working with lambdas. This guide focuses on some particular functional interfaces that are present in the java.util.function package.
🌐
Spring-soft
spring-soft.com › java_concepts › 2025 › 04 › 06 › exploring-javautilfunction.html
Exploring java.util.function Functional Interfaces in Java | SpringSoft, Where Innovation Meets Excellence
April 6, 2025 - // First function: Multiply the number by 2 Function<Integer, Integer> multiplyBy2 = x -> x * 2; // Second function: Add 3 to the number Function<Integer, Integer> add3 = x -> x + 3; // Composed function: Add 3 first, then multiply by 2 Function<Integer, Integer> composedFunction = ...
🌐
Java2Blog
java2blog.com › home › core java › java 8 › java 8 – java.util.function.function example
Java 8 - java.util.function.Function example - Java2Blog
January 26, 2021 - If you notice Stream’s map methods takes Function as input because map function requires a functional interface which takes input single argument T and returns result R and java.util.function.Function serves the purpose for Stream’s map method
🌐
Level Up Lunch
leveluplunch.com › java › examples › java-util-function-function-example
Java 8 function example | Level Up Lunch
March 9, 2014 - class Person { private int personId; ... } //.. } Function<Person, Job> mapPersonToJob = new Function<Person, Job>() { public Job apply(Person person) { Job job = new Job(person.getPersonId(), person.getJobDescription()); return job; } };...
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › Get-the-most-from-Java-Function-interface-with-this-example
A simple Java Function interface example: Learn Functional programming fast
January 30, 2025 - For this Java Function interface example, we will provide a single method named “apply” that takes an Integer as an argument, squares it and returns the result as a String. Before we begin, let’s take a quick look at the official JavaDoc for java.util.function.Function:
Find elsewhere
🌐
Oracle
docs.oracle.com › en › java › javase › 22 › docs › api › java.base › java › util › function › Function.html
Function (Java SE 22 & JDK 22)
July 16, 2024 - Package java.util.function · Type Parameters: T - the type of the input to the function · R - the type of the result of the function · All Known Subinterfaces: UnaryOperator<T> Functional Interface: This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
🌐
ExamClouds
examclouds.com › home › java core › lambda in java language
Functional Interface Function - Java Core
Function<T, R> is a built-in functional ... 8 in the java.util.function package. The main purpose of this interface is mapping scenarios - when an object of one type is taken as input, and it is converted (or mapped) to another type. The most common case for using the Function interface is in streams, where the map function of a stream accepts an instance of a Function to convert the stream of one type to a stream of another type. The definition of the interface is shown in the example...
🌐
Mkyong
mkyong.com › home › java8 › java 8 function examples
Java 8 Function Examples - Mkyong.com
February 27, 2020 - Java8Function1.java · package ... public class JavaMoney { public static void main(String[] args) { Function<String, Integer> func = x -> x.length(); Integer apply = func.apply("mkyong"); // 6 System.out.println(apply); } } Output ...
🌐
Java
download.java.net › java › early_access › panama › docs › api › java.base › java › util › function › Function.html
Function (Java SE 19 & JDK 19 [build 1])
Package java.util.function · Type Parameters: T - the type of the input to the function · R - the type of the result of the function · All Known Subinterfaces: UnaryOperator<T> Functional Interface: This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
🌐
Spring
docs.spring.io › spring-integration › reference › functions-support.html
java.util.function Interfaces Support :: Spring Integration
Meanwhile, an implementation of the Supplier interface can be used as regular MessageSource definition: @Bean public Function<String, String> toUpperCaseFunction() { return String::toUpperCase; } @Bean public Supplier<String> stringSupplier() { return () -> "foo"; } @Bean public IntegrationFlow ...
🌐
freeCodeCamp
freecodecamp.org › news › functional-programming-in-java
Functional Programming in Java
January 20, 2026 - In this article, we covered the implementation of functional programming (FP) concepts in Java, such as treating functions as first-class citizens, composing them into pipelines, and utilizing currying to simplify complex functions. We have provided examples using modern language features like java.util.function.Function and java.util.function.BiFunction, as well as a user-defined TriFunction with currying to overcome its limitations.
Top answer
1 of 3
40

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.

2 of 3
16

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());
}
🌐
Medium
medium.com › javaguides › java-function-functional-interface-with-real-world-examples-a1e86ea0fda5
Java Function Functional Interface with Real-World Examples | by Ramesh Fadatare | JavaGuides | Medium
March 1, 2025 - Learn how to use Java’s Function functional interface with a real-world example. Explore apply(), andThen(), compose(), and identity()…
🌐
Jenkov
jenkov.com › tutorials › java-functional-programming › functional-interfaces.html
Java Functional Interfaces
March 8, 2021 - First this example creates a new AddThree instance and assigns it to a Function variable. Second, the example calls the apply() method on the AddThree instance. Third, the example prints out the result (which is 7). You can also implement the ...