The LongUnaryOperator interface is a functional interface from the java.util.function package introduced in Java 8. It represents an operation that accepts one long argument and produces a long result.
- Useful for performing operations on
longvalues using lambda expressions. - Its main methods include
applyAsLong(),identity(),andThen(), andcompose().
Example: Program to use, LongUnaryOperator to calculate the square of a long value.
import java.util.function.LongUnaryOperator;
public class GFG {
public static void main(String[] args) {
LongUnaryOperator operation = n -> n * n;
System.out.println(operation.applyAsLong(6));
}
}
Output
36
Explanation: The lambda expression n -> n * n is executed when applyAsLong() receives 6.
Syntax
@FunctionalInterface
public interface LongUnaryOperator {
long applyAsLong(long operand);
}
Methods of LongUnaryOperator
The LongUnaryOperator interface provides methods to apply operations on long values and combine multiple operations using lambda expressions.
1. identity()
The identity() method returns an operator that produces the same long value supplied to it.
Syntax
static LongUnaryOperator identity()
import java.util.function.LongUnaryOperator;
public class GFG {
public static void main(String[] args) {
LongUnaryOperator operation = LongUnaryOperator.identity();
System.out.println(operation.applyAsLong(25));
}
}
Output
25
Explanation: identity() returns its input without changing it.
2. applyAsLong()
The applyAsLong() method applies the defined operation to a long value and returns the resulting long value.
Syntax
long applyAsLong(long operand)
import java.util.function.LongUnaryOperator;
public class GFG {
public static void main(String[] args)
{
LongUnaryOperator operation = n -> n + 10;
System.out.println(operation.applyAsLong(15));
}
}
Output
25
Explanation: The lambda adds 10 to the value passed to applyAsLong().
3. andThen()
The andThen() method creates a new operator that executes the current operation first and then applies the specified operation.
Syntax
default LongUnaryOperator andThen(LongUnaryOperator after)
import java.util.function.LongUnaryOperator;
public class GFG {
public static void main(String[] args) {
LongUnaryOperator operation = n -> n + 5;
LongUnaryOperator result =
operation.andThen(n -> n * 2);
System.out.println(result.applyAsLong(10));
}
}
Output
30
Explanation: First 5 is added to 10, and the resulting 15 is multiplied by 2.
Note: andThen() throws NullPointerException when the after operator is null.
4. compose()
The compose() method creates a new operator that executes the specified operation first and then applies the current operation.
Syntax
default LongUnaryOperator compose(LongUnaryOperator before)
import java.util.function.LongUnaryOperator;
public class GFG {
public static void main(String[] args) {
LongUnaryOperator operation = n -> n - 4;
LongUnaryOperator result =
operation.compose(n -> n * 3);
System.out.println(result.applyAsLong(10));
}
}
Output
26
Explanation: First 10 is multiplied by 3, giving 30, and then 4 is subtracted.
Note: compose() throws NullPointerException when the before operator is null.