IntUnaryOperator Interface in Java
Last Updated :
14 Apr, 2025
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 that takes in one argument and operates on it. Both its argument and return type are of the 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.
Different Methods of The IntUnaryOperator Interface
There are different kinds of methods present in the IntUnaryOperator interface, which is defined below:
1. identity()
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 any parameters.
- Returns: It returns an IntUnaryOperator, which takes in one value and returns it.
Example:
Java
import java.util.function.IntUnaryOperator;
public class Geeks
{
public static void main(String args[])
{
IntUnaryOperator op = IntUnaryOperator.identity();
System.out.println(op.applyAsInt(12));
}
}
2. applyAsInt()
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.
Example:
Java
import java.util.function.IntUnaryOperator;
public class Geeks
{
public static void main(String args[])
{
IntUnaryOperator op = a -> 2 * a;
System.out.println(op.applyAsInt(12));
}
}
3. andThen()
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.
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.
Example 1:
Java
import java.util.function.IntUnaryOperator;
public class Geeks
{
public static void main(String args[])
{
IntUnaryOperator op = a -> 2 * a;
op = op.andThen(a -> 3 * a);
System.out.println(op.applyAsInt(12));
}
}
Example 2:
Java
import java.util.function.IntUnaryOperator;
public class Geeks
{
public static void main(String args[])
{
IntUnaryOperator op = a -> 2 * a;
op = op.andThen(null);
System.out.println(op.applyAsInt(12));
}
}
Output:
4. compose()
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.
Example 1:
Java
import java.util.function.IntUnaryOperator;
public class Geeks
{
public static void main(String args[])
{
IntUnaryOperator op = a -> a / 3;
op = op.compose(a -> a * 6);
System.out.println(op.applyAsInt(12));
}
}
Example 2:
C++
import java.util.function.IntUnaryOperator;
public class Geeks
{
public static void main(String args[])
{
IntUnaryOperator op = a -> a / 3;
op = op.compose(null);
System.out.println(op.applyAsInt(12));
}
}
Output:
Similar Reads
Iterator Interface In Java Java Iterator Interface of java collections allows us to access elements of the collection and is used to iterate over the elements in the collection(Map, List or Set). It helps to easily retrieve the elements of a collection and perform operations on each element. Iterator is a universal iterator a
6 min read
IntStream map(IntUnaryOperator mapper) in Java IntStream map(IntUnaryOperator mapper) returns a stream consisting of the results of applying the given function to the elements of this stream. This is an intermediate operation. These operations are always lazy. Intermediate operations are invoked on a Stream instance and after they finish their p
2 min read
IntFunction Interface in Java with Examples The IntFunction 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 an int-valued argument and produces a result of type R. This functional interface takes in only one gener
1 min read
Java 8 | IntSupplier Interface with Examples The IntSupplier 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 does not take any argument but produces an int value. The lambda expression assigned to an object of IntSupplier t
1 min read
Marker Interface in Java In Java, a marker Interface is an empty interface that has no fields or methods. It is used just to mark or tag a class to tell Java or other programs something special about that class. These interfaces do not have any methods inside but act as metadata to provide information about the class. Examp
4 min read
Java Interface An Interface in Java programming language is defined as an abstract type used to specify the behaviour of a class. An interface in Java is a blueprint of a behaviour. A Java interface contains static constants and abstract methods. Key Properties of Interface:The interface in Java is a mechanism to
12 min read
Java Functional Interfaces A functional interface in Java is an interface that contains only one abstract method. Functional interfaces can have multiple default or static methods, but only one abstract method. Runnable, ActionListener, and Comparator are common examples of Java functional interfaces. From Java 8 onwards, lam
7 min read
Function Interface in Java The Function Interface is a part of the java.util.function package that has been introduced since Java 8, to implement functional programming in Java. It represents a function that takes in one argument and produces a result. Hence, this functional interface takes in 2 generics, namely as follows:T:
6 min read
Private Methods in Java 9 Interfaces Java 9 onwards, you can include private methods in interfaces. Before Java 9 it was not possible. Interfaces till Java 7 In Java SE 7 or earlier versions, an interface can have only two things i.e. Constant variables and Abstract methods. These interface methods MUST be implemented by classes which
4 min read
Java.util.function.LongBinaryOperator interface with Examples The LongBinaryOperator interface was introduced in Java 8. It represents an operation on two long values and returns the result as a long value. It is a functional interface and thus can be used as a lambda expression or in a method reference. It is mostly used when the operation needs to be encapsu
1 min read