UnaryOperator 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 as input but produces the output of same type as input argument. In this post, we are going to see several implementations of UnaryOperator Interface by using different examples.
Also, UnaryOperator extends Function interface therefore, it inherits apply(), compose() and andThen() functional methods from the Function.
UnaryOperator Interface in Java 8 with Examples...!!! Click To Tweet
Look at UnaryOperator Javadoc description below:
UnaryOperator Interface contains 1 method of it’s own and 3 extended methods:
identity
apply (extended from Function)
compose (extended from Function)
andThen (extended from Function)
Let’s discuss these methods:
identity
This method returns a unary operator that always returns its input argument.
1 2 3 | static <T> UnaryOperator<T> identity() { return t -> t; } |
apply
This method performs operation on the given argument and return the function result.
1 | R apply(T t); |
Compose
This method returns a composed function that first applies the before function to its input, and then applies this function to the result. If evaluation of either function throws an exception, it is relayed to the caller of the composed function.
1 2 3 4 | default <V> Function<V, R> compose(Function<? super V, ? extends T> before) { Objects.requireNonNull(before); return (V v) -> apply(before.apply(v)); } |
andThen
This method returns a composed function that first applies this function to its input, and then applies the after function to the result. If evaluation of either function throws an exception, it is relayed to the caller of the composed function.
1 2 3 4 | default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) { Objects.requireNonNull(after); return (T t) -> after.apply(apply(t)); } |
Below are the several examples to demonstrate above methods:
Example 1. ‘apply’
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import java.util.function.UnaryOperator; public class UnaryOperatorInterfaceJava8Example1 { public static void main(String[] args) { System.out.println("UnaryOperator Interface - 'apply' example \n"); UnaryOperator <Integer> obj1 = i -> i * 5; System.out.println("Value of Obj 1: " + obj1.apply(5)); UnaryOperator <String> obj2 = (s) -> s + "For Programmers & Entrepreneurs"; System.out.println(obj2.apply("Value of Obj 2: " + "Techndeck - ")); } } |
Output:
1 2 3 4 | UnaryOperator Interface - 'apply' example Value of Obj 1: 25 Value of Obj 2: Techndeck - For Programmers & Entrepreneurs |
Example 2. ‘andThen’
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import java.util.function.UnaryOperator; public class UnaryOperatorInterfaceJava8Example2 { public static void main(String[] args) { System.out.println("UnaryOperator Interface - 'apply' example \n"); UnaryOperator <Integer> obj1 = i -> i * 5; UnaryOperator <Integer> obj2 = i -> i + 5; //Using andThen System.out.println("Result: " + obj1.andThen(obj2).apply(4)); } } |
Output:
1 2 3 | UnaryOperator Interface - 'apply' example Result: 25 |
Example 3. ‘compose’
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import java.util.function.UnaryOperator; public class UnaryOperatorInterfaceJava8Example3 { public static void main(String[] args) { System.out.println("UnaryOperator Interface - 'apply' example \n"); UnaryOperator <Integer> obj1 = i -> i * 5; UnaryOperator <Integer> obj2 = i -> i + 5; //Using compose System.out.println("Result: " + obj1.compose(obj2).apply(4)); } } |
Output:
1 2 3 | UnaryOperator Interface - 'apply' example Result: 45 |
Java 8 UnaryOperator 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.
UnaryOperator Interface in Java 8 with Examples...!!! Click To Tweet
Do you like this Post? – then check my other helpful posts:
- Passing Function as a Parameter in another Method in Java 8
- Collection sorting using Lambda in Java 8
- Generate Prime numbers in Java 8
Other Useful References: