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_Java8_Techndeck

IntUnaryOperator Interface in Java 8 with Examples...!!! Share on X

Look at IntUnaryOperator Javadoc description below:

IntUnaryOperator_Java8_Signature_Techndeck

IntUnaryOperator Interface contains 4 methods:

  1. applyAsInt

  2. compose

  3. andThen

  4. identity

Let’s discuss these methods:

identity

This method returns a int unary operator that always returns its input argument.

static IntUnaryOperator identity() {
        return t -> t;
    }

applyAsInt 

This method performs operation on the given argument and return the function result.

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.

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.

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’

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:

IntUnaryOperator Interface - 'applyAsInt' example 

Value of Obj 1: 50

Example 2. ‘andThen’

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:

IntUnaryOperator Interface - 'andThen' example 

Result: 55

Example 3. ‘compose’

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:

IntUnaryOperator Interface - 'compose' example 

Result: 75

Java 8 IntUnaryOperator Interface is an absolute useful addition as part of ‘Functional Interfaces’ and can serve variety of purposes. It is quite powerful as it can be used as a higher order functions through lambda functions and above examples could help you to get better idea on how to implement it.

IntUnaryOperator Interface in Java 8 with Examples...!!! Share on X

Do you like this Post? – then check my other helpful posts:

Other Useful References:

Author

  • Deepak Verma

    Deepak Verma is a Test Automation Consultant and Software development Engineer for more than 10 years. His mission is to help you become an In-demand full stack automation tester.

    He is also the founder of Techndeck, a blog and online coaching platform dedicated to helping you succeed with all the automation basics to advanced testing automation tricks.

    View all posts