IntUnaryOperator Interface in Java

JavaObject Oriented ProgrammingProgramming

For functional programming in Java, the Java 9 version came with the IntUnaryOperator in Java, Let us see an example −

Example

 Live Demo

import java.util.function.IntUnaryOperator;
public class Demo{
   public static void main(String args[]){
      IntUnaryOperator op_1 = IntUnaryOperator.identity();
      System.out.println("The identity function :");
      System.out.println(op_1.applyAsInt(56));
      IntUnaryOperator op_2 = a -> 3 * a;
      System.out.println("The applyAsInt function :");
      System.out.println(op_2.applyAsInt(56));
      IntUnaryOperator op_3 = a -> 3 * a;
      System.out.println("The andThen function :");
      op_3 = op_3.andThen(a -> 5 * a);
      System.out.println(op_3.applyAsInt(56));
      IntUnaryOperator op_4 = a -> a / 6;
      System.out.println("The compose function :");
      op_4 = op_4.compose(a -> a * 9);
      System.out.println(op_4.applyAsInt(56));
   }
}

Output

The identity function :
56
The applyAsInt function :
168
The andThen function :
840
The compose function :
84

A class named ‘Demo’ contains the main function. Here, the ‘identity’ function is used on an instance of the ‘IntUnaryOperator’. Similarly, other functions such as ‘applyAsInt’, ‘andThen’, and ‘compose’ functions are used with newly created instances of the ‘IntUnaryOperator’. The output of every function call is printed on the console respectively.

raja
Published on 04-Jul-2020 08:43:12
Advertisements