Friday, August 17, 2018

Java8 - UnaryOperator interface Examples | Functional Interfaces tutorials

java.util.function.UnaryOperator

UnaryOperator is a functional interface in java.util.function package It operates on one operand and return the result of same operand type. This are assigned with lambdaexpressions/metod/constructor references

UnaryOperator code signature

@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

Lamda UnaryOperator method example 

This is an example of using UnaryOperator usage with lambda expressions
UnaryOperator<Integer> unaryOperator = value -> value* value;
System.out.println(unaryOperator.apply(6));
System.out.println(unaryOperator.apply(6));
Output is
49
16

Static Identity method example 

It has an only static method, which returns the input value. Below is an example for usage of this method
UnaryOperator<Integer> IdentityUnaryOperator = UnaryOperator.identity();
System.out.println(IdentityUnaryOperator.apply(25));
output is
25

Default methods andThen compose example 

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

Method Reference Example 

This is an example of a functional interface used with method reference
UnaryOperator<String> methodRef = String::toLowerCase;
System.out.println(methodRef.apply("CLOUDHADOOP"));
Output is

cloudhadoop

Primitive Unary Operators.

There are three primitive unary operators for Integer, Long and Double. All these contain only single abstract method. All these operators take corresponding value types and output same output type values. All this can be assigned with lambda expressions, passed arguments, method references All are defined in java.util.function package. All this will be used as a single arguments

IntUnaryOperator usage example 

It has an only single abstract method - applyAsInt It is a functional interface and takes an integer value and produces integer only
IntUnaryOperator IncrementInteger = (value) -> value +1;
System.out.println(IncrementInteger.applyAsInt(9));
The output of the above code is
10

LongUnaryOperator  interface usage

This is a functional interface and takes long as input and outputs long value only And the single abstract method is applyAsLong
LongUnaryOperator decrementLong = (value) -> value + 1;
System.out.println(decrementLong.applyAsLong(998756));
Output is
998757

DoubleUnaryOperator Usage Example

 DoubleUnaryOperator takes the double value and output double value only. This has only one abstract method - double applyAsDouble(double operand); Default methods - andThen, compose methods
DoubleUnaryOperator doubleUnaryOperatorExample = (value) -> value+value;
System.out.println(doubleUnaryOperatorExample.applyAsDouble(5));
System.out.println(doubleUnaryOperatorExample.applyAsDouble(25.2));
Output is
10.0
50.4

Related article


EmoticonEmoticon