Java 8 : UnaryOperator Functional Interface
UnaryOperator
UnaryOperator is a Function on a single operand that produces a result of the same type as its operand.
It Transforms the input value to a result of same type.
UnaryOperator Methods
UnaryOperator extends from Function. So, it inherits the Function interface methods apply(), andThen() and compose().
It also adds a static method identity() that returns a unary operator that always returns its input argument.
Example 1 :
package com.topjavatutorial; import java.util.function.UnaryOperator; public class TestUnaryOperator { public static void main(String[] args) { UnaryOperator<String> f1 = str -> str.toUpperCase(); System.out.println(f1.apply("hello")); } }
Output :
HELLO
Example 2 : UnaryOperator using Method Reference
package com.topjavatutorial; import java.util.function.UnaryOperator; public class TestUnaryOperator { public static void main(String[] args) { UnaryOperator<String> f2 = String::toUpperCase; System.out.println(f2.apply("hello")); } }
Output :
HELLO
© 2017, https:. All rights reserved. On republishing this post, you must provide link to original post