IntUnaryOperator Interface is a part of the java.util.function package which is introduced in Java 8. It is an in-built Functional Interface. This function expects an argument of type int as input but produces the output of same type as input argument. In this post, we are going to see several implementations of IntUnaryOperator Interface by using different examples.
IntUnaryOperator Interface in Java 8 with Examples...!!! Click To Tweet
Look at IntUnaryOperator Javadoc description below:
IntUnaryOperator Interface contains 4 methods:
applyAsInt
compose
andThen
identity
Let’s discuss these methods:
identity
This method returns a int unary operator that always returns its input argument.
1 2 3 | static IntUnaryOperator identity() { return t -> t; } |
applyAsInt
This method performs operation on the given argument and return the function result.
1 | int applyAsInt(int operand); |
Compose
This method returns a composed operator that first applies the before operator to its input, and then applies this operator to the result. If evaluation of either operator throws an exception, it is relayed to the caller of the composed operator.
1 2 3 4 | default IntUnaryOperator compose(IntUnaryOperator before) { Objects.requireNonNull(before); return (int v) -> applyAsInt(before.applyAsInt(v)); } |
andThen
This method returns a composed operator that first applies this operator to its input, and then applies the after operator to the result. If evaluation of either operator throws an exception, it is relayed to the caller of the composed operator.
1 2 3 4 | default IntUnaryOperator andThen(IntUnaryOperator after) { Objects.requireNonNull(after); return (int t) -> after.applyAsInt(applyAsInt(t)); } |
Below are the several examples to demonstrate above methods:
Example 1. ‘applyAsInt’
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import java.util.function.IntUnaryOperator; public class IntUnaryOperatorInterfaceJava8Example1 { public static void main(String[] args) { System.out.println("IntUnaryOperator Interface - 'applyAsInt' example \n"); IntUnaryOperator obj1 = d -> d * 5; System.out.println("Value of Obj 1: " + obj1.applyAsInt(10)); } } |
Output:
1 2 3 | IntUnaryOperator Interface - 'applyAsInt' example Value of Obj 1: 50 |
Example 2. ‘andThen’
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import java.util.function.IntUnaryOperator; public class IntUnaryOperatorInterfaceJava8Example2 { public static void main(String[] args) { System.out.println("IntUnaryOperator Interface - 'andThen' example \n"); IntUnaryOperator obj1 = d -> d * 5; IntUnaryOperator obj2 = d -> d + 5; System.out.println("Result: " + obj1.andThen(obj2).applyAsInt(10)); } } |
Output:
1 2 3 | IntUnaryOperator Interface - 'andThen' example Result: 55 |
Example 3. ‘compose’
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import java.util.function.IntUnaryOperator; public class IntUnaryOperatorInterfaceJava8Example3 { public static void main(String[] args) { System.out.println("IntUnaryOperator Interface - 'compose' example \n"); IntUnaryOperator obj1 = d -> d * 5; IntUnaryOperator obj2 = d -> d + 5; System.out.println("Result: " + obj1.compose(obj2).applyAsInt(10)); } } |
Output:
1 2 3 | IntUnaryOperator Interface - 'compose' example Result: 75 |
IntUnaryOperator Interface in Java 8 with Examples...!!! Click To Tweet
Do you like this Post? – then check my other helpful posts:
- Passing Function as a Parameter in another Method in Java 8
- Collection sorting using Lambda in Java 8
- Generate Prime numbers in Java 8