The UnaryOperator Interface<T> is a part of the java.util.function package which has been introduced since Java 8, to implement functional programming in Java. It represents a function which takes in one argument and operates on it. However what distinguishes it from a normal Function is that both its argument and return type are the same.
Hence this functional interface which takes in one generic namely:-
- T: denotes the type of the input argument to the operation
Hence the UnaryOperator<T> overloads the Function<T, T> type. So it inherits the following methods from the Function Interface:
- T apply(T t)
- default <V> Function<T, V> andThen(Function<? super R, ? extends V> after)
- default <V> Function<V, R> compose(Function<? super V, ? extends T> before)
The lambda expression assigned to an object of UnaryOperator type is used to define its accept() which eventually applies the given operation on its argument.
Functions in UnaryOperator Interface
The UnaryOperator interface consists of the following functions:
This method returns a UnaryOperator which takes in one value and returns it. The returned UnaryOperator does not perform any operation on its only value.
static UnaryOperator identity()
Parameters: This method does not take in any parameter.
Returns: A UnaryOperator which takes in one value and returns it.
Below is the code to illustrate accept() method:
Below are few examples to demonstrate the methods inherited from Function<T, T>:
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Stream iterate(T,Predicate,UnaryOperator) method in Java with examples
- Java 8 | Consumer Interface in Java with Examples
- Java 8 | BiConsumer Interface in Java with Examples
- Java 8 | DoubleToIntFunction Interface in Java with Example
- Java 8 | IntToDoubleFunction Interface in Java with Examples
- Java 8 | DoubleToLongFunction Interface in Java with Examples
- Java 8 | IntToLongFunction Interface in Java with Examples
- Java.util.function.BiPredicate interface in Java with Examples
- Java.util.function.DoublePredicate interface in Java with Examples
- Java.util.function.LongPredicate interface in Java with Examples
- Java.util.function.IntPredicate interface in Java with Examples
- Comparator Interface in Java with Examples
- Nested Interface in Java
- Java Interface methods
- Queue Interface In Java
- List Interface in Java with Examples
- SortedSet Interface in Java with Examples
- Map Interface in Java
- SortedMap Interface in Java with Examples
- NavigableMap Interface in Java with Example
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.