The IntUnaryOperator interface belongs to the java.util.function package and was introduced in Java 8. It represents an operation that accepts one int argument and produces an int result.
- It is a functional interface, so it works well with lambda expressions.
- The interface provides methods such as applyAsInt(), identity(), andThen(), and compose() for performing and combining operations.
Example: Program to demonstrates lambda expression defines the operation that is applied to the input value.
import java.util.function.IntUnaryOperator;
public class Geeks {
public static void main(String[] args) {
IntUnaryOperator operation = n -> n * 3;
System.out.println(operation.applyAsInt(10));
}
}
Output
30
Explanation: The lambda expression n -> n * 3 is executed when applyAsInt() receives 10.
Syntax
@FunctionalInterfacepublic interface IntUnaryOperator {int applyAsInt(int operand);}
Methods of IntUnaryOperator
The IntUnaryOperator interface provides methods to apply operations to int values and combine multiple operations using lambda expressions.
1. identity()
The identity() method returns an IntUnaryOperator that produces the same value supplied as input.
Syntax
static IntUnaryOperator identity()
import java.util.function.IntUnaryOperator;
public class Geeks {
public static void main(String[] args) {
IntUnaryOperator operation = IntUnaryOperator.identity();
System.out.println(operation.applyAsInt(20));
}
}
Output
20
Explanation: identity() returns the input value without modifying it.
2. applyAsInt()
The applyAsInt() method applies the operation defined by the IntUnaryOperator to an int value.
Syntax
int applyAsInt(int operand)
import java.util.function.IntUnaryOperator;
public class Geeks {
public static void main(String[] args) {
IntUnaryOperator operation = n -> n + 15;
System.out.println(operation.applyAsInt(10));
}
}
Output
25
Explanation: The lambda expression adds 15 to the input value.
3. andThen()
The andThen() method creates a composed operator that executes the current operation first and the specified operation afterward.
Syntax
default IntUnaryOperator andThen(IntUnaryOperator after)
import java.util.function.IntUnaryOperator;
public class Geeks {
public static void main(String[] args) {
IntUnaryOperator operation = n -> n * 2;
IntUnaryOperator result =
operation.andThen(n -> n + 5);
System.out.println(result.applyAsInt(10));
}
}
Output
25
Explanation: First 10 is multiplied by 2, producing 20, and then 5 is added to get 25.
Note: andThen() throws NullPointerException if the after operator is null.
4. compose()
The compose() method creates a composed operator that executes the specified operation first and the current operation afterward.
Syntax
default IntUnaryOperator compose(IntUnaryOperator before)
import java.util.function.IntUnaryOperator;
public class Geeks {
public static void main(String[] args) {
IntUnaryOperator operation = n -> n + 4;
IntUnaryOperator result =
operation.compose(n -> n * 2);
System.out.println(result.applyAsInt(10));
}
}
Output
24
Explanation: First 10 is multiplied by 2, and then 4 is added to the result.
Note: compose() throws NullPointerException if the before operator is null.