The UnaryOperator<T> interface is a part of the java.util.function package introduced in Java 8. It represents a function that takes only one argument, performs an operation on it, and returns a result of the same type. In simple words, input type and return type both remain the same in UnaryOperator.
- Specialized form of Function<T, T>
- Used to perform operations on a single value
- Frequently used with Lambda Expressions and Stream API
- Helps write concise and functional-style code
import java.util.function.UnaryOperator;
public class Main {
public static void main(String[] args) {
UnaryOperator<Integer> square = n -> n * n;
System.out.println(square.apply(5));
}
}
Output
25
Explanation:
- Input type = Integer
- Return type = Integer
- Since both types are same, UnaryOperator is used.
Syntax
UnaryOperator<T> operatorName = value -> operation;
T : Represents both the input type and return type.
Methods in UnaryOperator Interface
The UnaryOperator interface inherits methods from Function<T, T> interface.
1. apply()
The apply() method performs the operation defined in the lambda expression and returns the result.
Syntax:
T apply(T t)
- Parameters t : Input value
- Returns : Result after performing operation
import java.util.function.UnaryOperator;
public class Main {
public static void main(String[] args) {
UnaryOperator<Integer> cube = n -> n * n * n;
System.out.println(cube.apply(3));
}
}
Output
27
2. andThen()
The andThen() method first executes the current function and then executes the next function.
Syntax:
default <V> Function<T, V>andThen(Function<? super R, ? extends V> after)
- Parameters (after) : Function to execute after current function
- Returns : Combined function
import java.util.function.Function;
import java.util.function.UnaryOperator;
public class Main {
public static void main(String[] args) {
UnaryOperator<Integer> multiply = n -> n * 2;
UnaryOperator<Integer> add = n -> n + 5;
Function<Integer, Integer> result =
multiply.andThen(add);
System.out.println(result.apply(10));
}
}
Output
25
3. compose()
The compose() method first executes the given function and then executes the current function.
Syntax:
default <V> Function<V, R>compose(Function<? super V, ? extends T> before)
- Parameters (before) : Function to execute before current function
- Returns : Combined function
import java.util.function.Function;
import java.util.function.UnaryOperator;
public class Main {
public static void main(String[] args) {
UnaryOperator<Integer> multiply = n -> n * 2;
UnaryOperator<Integer> add = n -> n + 5;
Function<Integer, Integer> result =
multiply.compose(add);
System.out.println(result.apply(10));
}
}
Output
30
4. identity()
This method returns a UnaryOperator that simply returns the same value passed to it. It does not perform any modification.
Syntax:
static <T> UnaryOperator<T> identity()
- Parameters : No parameter
- Returns : Same input value
import java.util.function.UnaryOperator;
public class Main {
public static void main(String[] args) {
UnaryOperator<String> op =
UnaryOperator.identity();
System.out.println(op.apply("Java"));
}
}
Output
Java
Difference Between andThen() and compose()
| Method | Execution Order |
|---|---|
| andThen() | Current function -> Next function |
| compose() | Given function -> Current function |