IntUnaryOperator Interface in Java


IntUnaryOperator Interface, a functional interface of Java that performs an operation on a single integer-valued operand and also returns an integer value as a result. Since it is a functional interface, we can use it as an assignment target for the lambda expressions or method references. Here, the functional interface means an interface that contains only a single abstract method and exhibits single functionality. Some examples of functional interfaces are Predicate, Runnable, and Comparable interfaces. The IntUnaryOperator interface is defined in the 'java.util.function' package. In this article, we are going to explore the IntUnaryOperator Interface and its built-in methods with the help of example programs.

IntUnaryOperator Interface in Java

In Java, the IntUnaryOperator Interface provides the following methods −

  • applyAsInt()

  • identity()

  • compose()

  • andThen()

We will discuss these methods one by one, but before that let's see the syntax of IntUnaryOperator Interface.

Syntax

public interface IntUnaryOperator

It is necessary to import this Interface before using it in our program. To import the IntUnaryOperator Interface use the following command −

import java.util.function.IntUnaryOperator;

Use of applyAsInt() method of IntUnaryOperator

It is the only abstract method of IntUnaryOperator Interface which takes an integer as a parameter and returns an integer as a result after applying the operator to the given operand.

Syntax

int applyAsInt(int argument);

Example 

The following example illustrates the practical implementation of applyAsInt() method.

import java.util.function.IntUnaryOperator;
public class Example1 {
   public static void main(String []args) {
      IntUnaryOperator op_1 = a -> 3 * a; // instance of IntUnaryOperator
      System.out.println("The applyAsInt function :");
      // using applyAsInt method  
      System.out.println(op_1.applyAsInt(56));
   }
}

Output

The applyAsInt function :
168

Use of identity() method of IntUnaryOperator

It is a static method of IntUnaryOperator Interface in Java that returns a unary operator that always returns its given argument.

Syntax

IntUnaryOperator.identity();

Example 

In the following example, we will show the use of identity() method.

import java.util.function.IntUnaryOperator;
public class Example2 {
   public static void main(String []args) {
      // creating instance of IntUnaryOperator using identity()
      IntUnaryOperator op_2 = IntUnaryOperator.identity();
      System.out.println("The identity function :");
      // calling applyAsInt method
      System.out.println(op_2.applyAsInt(56));
   }
}

Output

The identity function :
56

Use of compose() method of IntUnaryOperator

It always returns a composed IntUnaryOperator after evaluating the argument passed to this method. Next, the first IntUnaryOperator will be evaluated by applying this composed IntUnaryOperator.

Syntax

instance.compose(expression);

Example 

The following example shows the use of compose() method. When we pass the operand, first, compose() method will get executed then the result will be applied to the original instance of IntUnaryOperator.

import java.util.function.IntUnaryOperator;
public class Example3 {
   public static void main(String []args) {
      // creating instance of IntUnaryOperator 
      IntUnaryOperator op_3 = a -> a / 6;
      System.out.println("The compose function :");
      // calling compose method
      op_3 = op_3.compose(a -> a * 9);
      // to print the result
      System.out.println(op_3.applyAsInt(56));
   }
}

Output

The compose function :
84

Use of addThen() method of IntUnaryOperator

Like compose() method, it also returns a composed IntUnaryOperator of the given operation. But, when we use addThen() method the original IntUnaryOperator will be evaluated first and then the expression of addThen() method.

Syntax

instance.addThen(expression);

Example 

In the following example, we will demonstrate the use of addThen() method.

import java.util.function.IntUnaryOperator;
public class Example4 {
   public static void main(String []args) {
      // creating instance of IntUnaryOperator 
      IntUnaryOperator op_4 = a -> 3 * a;
      System.out.println("The andThen function :");
      // calling addThen method
      op_4 = op_4.andThen(a -> 5 * a);
      // to print the result
      System.out.println(op_4.applyAsInt(56));
   }
}

Output

The andThen function :
840

Conclusion

In this article, we have learned about the IntUnaryOperator Interface and its built-in methods. It has only one abstract method named 'applyAsInt()' that takes an integer argument and returns an integer as a result. The other two methods 'andThen()' and 'compose()' methods return a composed IntUnaryOperator of the specified operation.

Updated on: 10-Aug-2023

122 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements