The LongFunction
is a functional interface in Java present in the java.util.function
package. It represents a function that takes a long
as input and produces a result of a specified type.
Key points about LongFunction
:
1. Single Abstract Method:
It contains a single abstract method:
R apply(long value);
Here, R
is the return type of the function.
2. Functional Interface:
As a functional interface, it can be used with lambda expressions, method references, or anonymous classes.
Usage of LongFunction
We use LongFunction
when we want to process a long
value and return a result of a specific type.
Example 1: Simple Lambda Expression
Here’s an example where we convert a long
to its string representation:
package org.kodejava.util.function;
import java.util.function.LongFunction;
public class LongFunctionExample {
public static void main(String[] args) {
// Create a LongFunction that converts long to String
LongFunction<String> longToString = (long value) -> "Value: " + value;
// Apply the function
String result = longToString.apply(25L);
System.out.println(result);
// Output: Value: 25
}
}
Example 2: Use with Streams
We can use LongFunction
with streams, especially when working with LongStream
.
package org.kodejava.util.function;
import java.util.function.LongFunction;
import java.util.stream.LongStream;
public class LongFunctionWithStream {
public static void main(String[] args) {
// Create a LongFunction that converts a long to its square formatted as a String
LongFunction<String> longToSquareString =
(long value) -> "Square of " + value + " is " + (value * value);
// Use it in a LongStream
LongStream.range(1, 5)
.mapToObj(longToSquareString)
.forEach(System.out::println);
}
}
Output:
Square of 1 is 1
Square of 2 is 4
Square of 3 is 9
Square of 4 is 16
Example 3: Using a Method Reference
We can use method references with LongFunction
as well. For instance:
package org.kodejava.util.function;
import java.util.function.LongFunction;
public class LongFunctionMethodReference {
public static void main(String[] args) {
// Method reference for a custom static method
LongFunction<String> longToString = LongFunctionMethodReference::customFormatter;
// Apply the function
System.out.println(longToString.apply(26L));
// Output: Custom Value: 26
}
// Custom static method
public static String customFormatter(long value) {
return "Custom Value: " + value;
}
}
Practical Use Cases
Some scenarios where LongFunction
can be useful:
1. Transforming numerical IDs: Converting long IDs (e.g., user or record IDs) into their string descriptions.
2. Processing large numerical data: When working with LongStream
, LongFunction
can help in transforming long
values into complex objects.
3. Mapping long values to specific results: E.g., mapping employee IDs to employee details.
Summary
LongFunction is a versatile functional interface designed for processing long
inputs and returning a result of any type. It can be easily used in conjunction with lambdas, method references, and streams to write compact and expressive code.
- How do I use the LongToDoubleFunction functional interface in Java? - March 15, 2025
- How do I use the LongSupplier functional interface in Java? - March 14, 2025
- How do I use the LongPredicate functional interface in Java? - March 14, 2025