Java8 UnaryOperator interface Examples tutorials

What is UnaryOperator in Java?

UnaryOperator is a functional interface within the java.util.function package designed to operate on a single operand and return a result of the same operand type.

These operators are utilized with lambda expressions or method and constructor references.

UnaryOperator Java 8 Examples

Here is an declaration of the UnaryOperator interface

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

UnaryOperator extends the Function interface and inherits its abstract apply method. The Function interface also provides default andThen() and compose methods.

Java 8 UnaryOperator Example with Lambda Expression

Here’s an example demonstrating the usage of UnaryOperator 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

The UnaryOperator interface includes a static identity method, which simply returns the input value.

Here’s an example:

import java.util.function.UnaryOperator;

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

output:

25

Default Methods andThen and compose

These methods are inherited from the Function interface. The difference lies in the order of parameter execution: andThen runs the called function first and the parameter last, while compose runs the parameter first and the 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

Java 8 Unary Operator Method Reference Example

Here’s an example of using a method reference with the UnaryOperator functional interface:

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 Java 8

Java 8 introduces primitive unary operators for Integer, Long, and Double, defined in the java.util.function package. All these contain only a single abstract method.

These operators accept numeric values as input and produce values of the same type.

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 Java 8

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

LongUnaryOperator Interface Example in Java 8

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

DoubleUnaryOperator

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

This tutorial covered the UnaryOperator interface in Java 8, including examples utilizing lambda expressions, method references, and primitive unary operators.

Gain insights into effectively utilizing unary operators in your Java applications for enhanced functionality and efficiency.