The IntUnaryOperator is a functional interface in Java that resides in the java.util.function package. It represents a function that accepts a single int-valued argument and produces an int-valued result. It is often used when working with Lambda expressions or method references where we need to process integers.
Here’s a detailed explanation of how to use IntUnaryOperator with examples:
1. Signature of IntUnaryOperator
The IntUnaryOperator interface has a single abstract method:
int applyAsInt(int operand);
This method takes an int as input and returns another int as output.
2. Syntax and Lambda Expression
We can use IntUnaryOperator by providing an implementation of the applyAsInt method, typically through a lambda expression or method reference.
// Doubles the input value
IntUnaryOperator operator = (int x) -> x * 2;
// Result is 10
int result = operator.applyAsInt(5);
3. Static Methods Available
The interface also provides some default or additional static methods to combine or chain operations:
a. compose method
The compose method allows we to first apply another IntUnaryOperator and then apply the current operator.
IntUnaryOperator operator1 = x -> x + 3; // Adds 3
IntUnaryOperator operator2 = x -> x * 2; // Doubles the value
// First apply 'operator1', then 'operator2'
IntUnaryOperator combined = operator2.compose(operator1);
// Result is (5 + 3) * 2 = 16
int result = combined.applyAsInt(5);
b. andThen method
This method allows we to first apply the current operator and then apply another operator.
IntUnaryOperator operator1 = x -> x + 3; // Adds 3
IntUnaryOperator operator2 = x -> x * 2; // Doubles the value
// First apply 'operator1', then 'operator2'
IntUnaryOperator combined = operator1.andThen(operator2);
// Result is (5 + 3) * 2 = 16
int result = combined.applyAsInt(5);
c. identity method
The identity method returns an IntUnaryOperator that always returns its input value unchanged.
IntUnaryOperator identityOperator = IntUnaryOperator.identity();
// Result is 10
int result = identityOperator.applyAsInt(10);
4. Use Cases
- Mapping values in an array or collection:
package org.kodejava.util.function;
import java.util.Arrays;
import java.util.function.IntUnaryOperator;
public class MappingValueExample {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
IntUnaryOperator doubleOperator = x -> x * 2;
int[] doubledNumbers = Arrays.stream(numbers)
.map(doubleOperator)
.toArray();
// Output: [2, 4, 6, 8, 10]
System.out.println(Arrays.toString(doubledNumbers));
}
}
- Chaining multiple operations:
package org.kodejava.util.function;
import java.util.function.IntUnaryOperator;
public class ChainOperationExample {
public static void main(String[] args) {
int number = 5;
IntUnaryOperator addFive = x -> x + 5;
IntUnaryOperator square = x -> x * x;
IntUnaryOperator combinedOperator = addFive.andThen(square);
int result = combinedOperator.applyAsInt(number); // (5 + 5)² = 100
// Output: Result: 100
System.out.println("Result: " + result);
}
}
5. Key Benefits
- Simplifies working with single-argument integer operations.
- Performs operations without boxing/unboxing overhead, as it uses primitive
intinstead ofInteger. - Easily composable with methods like
composeandandThen.
This functional interface is useful in various scenarios, especially when we want to perform operations or transformations on primitive int values in a concise manner.
