🌐
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 ...
🌐
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 - // Java Program to illustrate addThen() method // Importing interface import java.util.function.Function; // Main class public class Geeks { // main driver method public static void main(String args[]) { // Function which takes in a number and // returns half of it Function<Integer, Double> half = a -> a / 2.0; // Now triple the output of half function half = half.andThen(a -> 3 * a); // Applying the function to get the result // and printing on console System.out.println(half.apply(10)); } } ... Example 2: To demonstrate when NullPointerException is returned.
🌐
Baeldung
baeldung.com › home › java › core java › functional interfaces in java
Functional Interfaces in Java | Baeldung
March 26, 2025 - This tutorial is a guide to different functional interfaces present in Java 8, as well as their general use cases, and usage in the standard JDK library.
🌐
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 = ...
🌐
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 also has one function apply (), this function takes two input parameters as T and U and returns a value as R after performing some kind of operation on given input parameters. ... This T, U, and R may have any type of value like Integer, Float, Double, String, etc. Let’s understand the use of it by given example:
🌐
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
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:
🌐
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...
🌐
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".
Find elsewhere
🌐
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
🌐
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.
🌐
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...
🌐
Spring
docs.spring.io › spring-integration › reference › functions-support.html
java.util.function Interfaces Support :: Spring Integration
With the Java DSL we just need to use a reference to the function bean in the endpoint definitions. 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 supplierFlow() { return IntegrationFlow.from(stringSupplier()) .transform(toUpperCaseFunction()) .channel("suppliedChannel") .get(); }
🌐
Android Developers
developer.android.com › api reference › java.util.function
java.util.function | API reference | Android Developers
Skip to main content · English · Deutsch · Español – América Latina · Français · Indonesia · Polski · Português – Brasil · Tiếng Việt · 中文 – 简体
🌐
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; } };...
🌐
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.
🌐
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.
🌐
Medium
medium.com › @ashaythecoder › part-b-java-util-function-api-of-java-8-5b6533996075
Part B: java.util.function API of Java-8 | by Ashay Patil | Medium
December 14, 2021 - This functional interface contains only one abstract method test(). This Functional Interface is mainly used in Stream API’s filter operation, where we filter out elements from Stream based on condition check. ... In above example, we have written predicate to check whether Employee is eligible for bonus based on (Employee’s rating > 10) Condition.
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());
}