IntUnaryOperator Interface in Java - GeeksforGeeks

18 Nov, 2019
The IntUnaryOperator Interface is a part of the java.util.function package which has been introduced since Java 8, to implement functional programming in Java. It represents a function which takes in one argument and operates on it. Both its argument and return type are of int data type.It is very similar to using an object of type UnaryOperator<Integer>.
The lambda expression assigned to an object of IntUnaryOperator type is used to define its applyAsInt() which eventually applies the given operation on its argument.
The IntUnaryOperator interface consists of the following functions:
This method returns an IntUnaryOperator which takes in one int value and returns it. The returned IntUnaryOperator does not perform any operation on its only value.
Syntax:
static  IntUnaryOperator identity()
Parameters: This method does not take in any parameter
Returns: A IntUnaryOperator which takes in one value and returns it.
Below is the code to illustrate identity() method:
Program
import java.util.function.IntUnaryOperator;  public class GFG {    public static void main(String args[])    {        IntUnaryOperator op = IntUnaryOperator.identity();        System.out.println(op.applyAsInt(12));    }}
12

This method takes in one int value, performs the given operation and returns an int-valued result.
Syntax:
int applyAsInt(int operand)
Parameters: This method takes in one int valued parameter
Returns:: It returns an int valued result.
Below is the code to illustrate applyAsInt() method:
import java.util.function.IntUnaryOperator;  public class GFG {    public static void main(String args[])    {        IntUnaryOperator op = a -> 2 * a;        System.out.println(op.applyAsInt(12));    }}
24

It returns a composed IntUnaryOperator wherein the parameterized operator will be executed after the first one. If either operation throws an error, it is relayed to the caller of the composed operation.
Syntax:
default IntUnaryOperator andThen(IntUnaryOperator after)
Parameters: This method accepts a parameter after which is the operation to be applied after the current one.
Return Value: This method returns a composed IntUnaryOperator that applies the current operation first and then the after operation.
Exception: This method throws NullPointerException if the after operation is null.
Below is the code to illustrate addThen() method:
Program 1:
import java.util.function.IntUnaryOperator;  public class GFG {    public static void main(String args[])    {        IntUnaryOperator op = a -> 2 * a;        op = op.andThen(a -> 3 * a);        System.out.println(op.applyAsInt(12));    }}
72

Program 2: To demonstrate when NullPointerException is returned.
import java.util.function.IntUnaryOperator;  public class GFG {    public static void main(String args[])    {        IntUnaryOperator op = a -> 2 * a;        op = op.andThen(null);        System.out.println(op.applyAsInt(12));    }}
java.lang.NullPointerException

It returns a composed IntUnaryOperator wherein the parameterized operation will be executed first and then the first one. If either operation throws an error, it is relayed to the caller of the composed operation.
Syntax:
default IntUnaryOperator compose(IntUnaryOperator before)
Parameters: This method accepts a parameter before which is the operation to be applied first and then the current one
Return Value: This method returns a composed IntUnaryOperator that applies the current operator after the parameterized operator
Exception: This method throws NullPointerException if the before operation is null.
Below is the code to illustrate compose() method:
Program 1:
import java.util.function.IntUnaryOperator;  public class GFG {    public static void main(String args[])    {        IntUnaryOperator op = a -> a / 3;        op = op.compose(a -> a * 6);        System.out.println(op.applyAsInt(12));    }}
24

Program 2: To demonstrate when NullPointerException is returned.
import java.util.function.IntUnaryOperator;  public class GFG {    public static void main(String args[])    {        IntUnaryOperator op = a -> a / 3;        op = op.compose(null);        System.out.println(op.applyAsInt(12));    }}
java.lang.NullPointerException

Akanksha_Rai
Java - util package
Java 8
java-basics
Java-Functional Programming
java-interfaces
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Object Oriented Programming (OOPs) Concept in Java
HashMap in Java with Examples
Interfaces in Java
How to iterate any Map in Java
Initialize an ArrayList in Java
ArrayList in Java
Stack Class in Java
Singleton Class in Java
Multidimensional Arrays in Java
Set in Java