One usage of Function is in Streams. Everyone uses map method these days, I believe:

This map method accepts the Function as a parameter. This allows writing a pretty elegant code - something that could not be achieved before Java 8:

Stream.of("a", "b", "c")
   .map(s -> s.toUpperCase())
   .collect(Collectors.toList());
// List of A, B, C

Now its true that there are method references and functional interfaces (one of which is Function of course), this lets you using method reference to rewrite the above example as:

Stream.of("a", "b", "c")
    .map(String::toUpperCase)
    .collect(Collectors.toList())

... but that's only a syntactic sugar - map still accepts the Function as a parameter of course.

Another example that uses Function from Java itself is StackWalker: Here is an example:

List<StackFrame> frames = StackWalker.getInstance().walk(s ->
    s.dropWhile(f -> f.getClassName().startsWith("com.foo."))
     .limit(10)
     .collect(Collectors.toList()));
}

Note the call to walk method - it accepts a function as a parameter.

So bottom line, it's just yet another tool that can help the programmer to express his/her intentions. Use it wisely wherever appropriate.

Answer from Mark Bramnik on Stack Overflow
🌐
Mkyong
mkyong.com › home › java8 › java 8 function examples
Java 8 Function Examples - Mkyong.com
February 27, 2020 - package com.mkyong; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.function.Function; public class Java8Function3 { public static void main(String[] args) { Java8Function3 obj = new Java8Function3(); List<String> list = Arrays.asList("node", "c++", "java", "javascript"); // lambda Map<String, Integer> map = obj.convertListToMap(list, x -> x.length()); System.out.println(map); // {node=4, c++=3, java=4, javascript=10} // method reference Map<String, Integer> map2 = obj.convertListToMap(list, obj::getLength); System.out.println(ma
🌐
GeeksforGeeks
geeksforgeeks.org › java › function-interface-in-java
Function Interface in Java - GeeksforGeeks
July 11, 2025 - Return Type: This method returns the function result, which is of type R. Example: Java ·
People also ask

How do you define a function in Java?
In Java, you define a function using a method within a class. The syntax includes an access modifier, return type, method name, parameter list in parentheses, and a body enclosed in braces. For example: `public int add(int a, int b) { return a + b; }`.
🌐
vaia.com
vaia.com › java function
Java Function: Definition & Examples | Vaia
What is the difference between a function and a method in Java?
In Java, a function is a concept representing a block of code executed to perform a task, usually found independently in languages like C. A method, however, is a function defined within a class. In Java, functions are considered methods since Java is a purely object-oriented language. Methods have a relationship with objects and classes.
🌐
vaia.com
vaia.com › java function
Java Function: Definition & Examples | Vaia
How do you call a function in Java?
To call a function in Java, use the function name followed by parentheses. If the function requires parameters, include them within the parentheses. For example, `functionName()` or `functionName(argument1, argument2)` if parameters are needed. Call the function from within a method like `main`.
🌐
vaia.com
vaia.com › java function
Java Function: Definition & Examples | Vaia
🌐
W3Schools
w3schools.com › java › java_methods.asp
Java Methods
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... A method is a block of code which only runs when it is called. You can pass data, known as parameters, into a method. Methods are used to perform certain actions, and they are also known as functions...
🌐
DEV Community
dev.to › devcorner › functional-programming-in-java-1pn3
Functional Programming in Java - DEV Community
March 7, 2025 - Introduced in Java 8, the Streams API provides a functional-style approach to processing collections of data.
🌐
Medium
medium.com › @kavya1234 › understanding-functional-interfaces-in-java-8-25a15ea7982b
Understanding Functional Interfaces in Java 8 | by Kavya | Medium
June 10, 2024 - In this example, Calculator is a custom functional interface with a single abstract method calculate. Lambda expressions are used to provide implementations for addition and subtraction operations. Functional interfaces are extensively used with Java Collections to perform various operations ...
Find elsewhere
🌐
Vaia
vaia.com › java function
Java Function: Definition & Examples | Vaia
December 13, 2024 - A String is a sequence of characters, which in Java is represented by the String class. It is immutable, meaning its value cannot be changed once created. ... split(String regex): Splits string into an array based on a regular expression.These functions help in effectively managing and manipulating string data. Consider an example demonstrating several string methods:
🌐
Java Concept Of The Day
javaconceptoftheday.com › home › java 8 functional interfaces – when & how to use them?
Java 8 Functional Interfaces - When & How To Use Them?
March 17, 2019 - Java 8 has also introduced functional interfaces which support primitive types. For example IntPredicate, DoublePredicate, LongConsumer etc…
🌐
Medium
medium.com › developers-yesterday › java-tutorial-functions-3c021a6d78c6
Java Tutorial. Functions. What is a function and how are they… | by Emmanuel Tejeda | Developers Yesterday | Medium
July 29, 2022 - In Java, and almost all programming languages, we use functions to make our code neater, more compact and easier to manage. In this tutorial I will show you how functions accomplish these three goals. First let’s define what a function is. A function is a block of code that performs a specific task. They can be reused as many times as needed and they are normally used to pass information and receive information back. Let’s give an example ...
🌐
DZone
dzone.com › coding › languages › functional programming in java 8 (part 1): functions as objects
Functional Programming in Java 8 (Part 1)
January 26, 2017 - That’s pretty straightforward, isn’t it? BiFunction is another Interface in java.util to represent a function with two arguments and one return object. In the brackets of the Lambda, you define the arguments. You don’t have to give them a type. You just have to say how many there are and how each should be called.
🌐
DZone
dzone.com › coding › java › functional programming with java 8 functions
Functional Programming with Java 8 Functions
October 20, 2014 - This is a classical example of what is called function composition. In some languages there is even a binary operator to compose two functions in this way: ... Where o would be an operator that would compose functions f and g pretty much as we did in pseudocode above and produce a new function h. ... I can think of two ways to do this in Java...
🌐
Coderanch
coderanch.com › t › 782972 › java › practive-functional-interfaces
Best practive to use functional interfaces (Java in General forum at Coderanch)
July 18, 2024 - hi Björn, predicates, and in general functional interfaces, seem complicated at first, but when you get the hang of it you wonder how you ever managed without them. I wrote a very simplified and perhaps clumsy example, that uses predicates, maps, consumers and an enum, and that gets the job done without any if/then/else.
🌐
Programiz
programiz.com › java-programming › methods
Java Methods (With Examples)
August 26, 2024 - In the above example, we have created a method named addNumbers(). The method takes two parameters a and b. Notice the line, ... Here, we have called the method by passing two arguments num1 and num2. Since the method is returning some value, we have stored the value in the result variable. Note: The method is not static. Hence, we are calling the method using the object of the class. A Java method may or may not return a value to the function ...
🌐
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
When you run the inner class example, the output would be nine. Of course, the whole idea of the functional interface is to incorporate lambda expressions into the mix. Here’s the Java Function example implemented with a rather verbose lambda expression:
🌐
Learn Java
learnjavaonline.org › en › Functions
Functions - Learn Java - Free Interactive Java Tutorial
I always like to say that arguments to Java methods are passed by value, although some might disagree with my choice of words, I find it the best way to explain and understand how it works exactly. By value means that arguments are copied when the method runs. Let's look at an example.
🌐
Dremendo
dremendo.com › java-programming-tutorial › java-function
Function in Java Programming | Dremendo
In Pass by Value the values of the variables are passed to the formal arguments of the function. In this case the values of the actual arguments are not affected by changing the values of the formal arguments. See the example given below. import java.util.Scanner; public class Example { public static void changevalue(int a,int b) { a=a+2; b=b+2; System.out.println("In function changes are " + a + " and " + b); } public static void main(String args[]) { int x=10,y=20; System.out.println("Before calling the function"); System.out.println("x=" + x + " and y=" + y); changevalue(x,y); System.out.println("After calling the function"); System.out.println("x=" + x + " and y=" + y); } }
🌐
ConcretePage
concretepage.com › java › jdk-8 › java-8-function-examples
Java Function with Examples
Accepts only integer value and ....function; import java.util.function.IntFunction; public class IntFunctionExample { public static void main(String[] args) { IntFunction ob = f -> f*f; System.out.println(ob.apply(43)); } }...