Java8 UnaryOperator interface Examples tutorials

What is UnaryOperator in java?

UnaryOperator is a functional interface in java.util.function package. It operates on one operand and returns the result of the same operand type.

These are assigned with lambda expressions or method or constructor references.

UnaryOperator java8 examples

Here is an UnaryOperator interface declaration

@FunctionalInterface
public interface UnaryOperator<T> extends Function<T, T> {
    static <T> UnaryOperator<T> identity() {
        return t -> t;
    }
}

UnaryOperator extends Function interface. It extends abstract apply from the functional interface. Function Interface has default andThen() and compose methods

java8 UnaryOperator example using lambda expression

It is an example of using UnaryOperator usage with lambda expressions.

import java.util.function.UnaryOperator;

public class Test {
    public static void main(String[] args)  throws Exception{
        UnaryOperator<Integer> unaryOperator = value -> value* value;
        System.out.println(unaryOperator.apply(6));
        System.out.println(unaryOperator.apply(6));
    }
}

Output:

49
16

It has static identity method

Static Identity method

It has only a static method, which returns the input value. Below is an example of the usage of this method

import java.util.function.UnaryOperator;

public class IdentityUnaryOperatorExample {
    public static void main(String[] args)  throws Exception{
        UnaryOperator<Integer> IdentityUnaryOperator = UnaryOperator.identity();
        System.out.println(IdentityUnaryOperator.apply(25));

    }
}

output:

25

Default methods andThen compose

This method extended from the Function interface.

The difference between andThen and compose is the order of the parameter execution andThen - Run the called function first and parameter last compose - Run the parameter first and called function last.

import java.util.function.UnaryOperator;

public class Test {
    public static void main(String[] args)  throws Exception{
        UnaryOperator<Integer> UnaryOperator1 = value -> value * value;
        UnaryOperator<Integer> UnaryOperator2 = value -> value * 10;
        System.out.println(UnaryOperator1.andThen(UnaryOperator2).apply(3));
        System.out.println(UnaryOperator1.compose(UnaryOperator2).apply(3));
    }
}

The output of the above code execution is

90
900

java8 Unary Operator Method Reference Example

This is an example of a functional interface used with method reference

import java.util.function.UnaryOperator;

public class Test {
    public static void main(String[] args)  throws Exception{
        UnaryOperator<String> methodRef = String::toLowerCase;
        System.out.println(methodRef.apply("CLOUDHADOOP"));
    }
}

Output:

cloudhadoop

Primitive Unary Operators in java8

There are three primitive unary operators for Integer, Long and Double, defined in the java.util.function package.

All these contain only a single abstract method. All these operators take input numeric values and output the same output type values.

These can be assigned with lambda expressions, passed as arguments, method references.

All this will be used as a single argument.

IntUnaryOperator interface example in java8

It has only a single abstract method - applyAsInt. It is a functional interface and takes an integer value and produces integer only.

import java.util.function.IntUnaryOperator;

public class IntUnaryOperatorExample {
    public static void main(String[] args)  throws Exception{
        IntUnaryOperator IncrementInteger = (value) -> value +1;
        System.out.println(IncrementInteger.applyAsInt(9));
    }
}

The output of the above code is

10

java8 LongUnaryOperator interface example

It is a functional interface, and takes longer as the input and outputs long value only. And the single abstract method is applyAsLong.

import java.util.function.LongUnaryOperator;

public class LongUnaryOperatorExample {
    public static void main(String[] args)  throws Exception{
        LongUnaryOperator decrementLong = (value) -> value + 1;
        System.out.println(decrementLong.applyAsLong(998756));
    }
}

Output:

998757

java8 DoubleUnaryOperator interface Example

DoubleUnaryOperator takes the double value and output double value only. It has only one abstract method - double applyAsDouble(double operand); Default methods - andThen, compose methods.

import java.util.function.DoubleUnaryOperator;

public class DoubleUnaryOperatorExample {
    public static void main(String[] args)  throws Exception{
        DoubleUnaryOperator doubleUnaryOperatorExample = (value) -> value+value;
        System.out.println(doubleUnaryOperatorExample.applyAsDouble(5));
        System.out.println(doubleUnaryOperatorExample.applyAsDouble(25.2));
    }
}

Output:

10.0
50.4

Conclusion

In this tutorial, Learned primitive UnaryOperator and examples using lambda expression and method reference.

It also includes examples for LongUnaryOperator,DoubleUnaryOperator,IntUnaryOperator