UnaryOperator Interface in Java Last Updated : 18 Nov, 2019 Comments Improve Suggest changes Like Article Like Report 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: 1. identity() 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. Syntax: 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: Program 1: Java import java.util.function.UnaryOperator; public class GFG { public static void main(String args[]) { // Instantiate the UnaryOperator interface UnaryOperator<Boolean> op = UnaryOperator.identity(); // Apply identify() method System.out.println(op.apply(true)); } } Output: true Below are few examples to demonstrate the methods inherited from Function<T, T>: 1.accept() Java import java.util.function.Function; import java.util.function.UnaryOperator; public class GFG { public static void main(String args[]) { UnaryOperator<Integer> xor = a -> a ^ 1; System.out.println(xor.apply(2)); } } Output: 3 2.addThen() Java import java.util.function.Function; import java.util.function.UnaryOperator; public class GFG { public static void main(String args[]) { UnaryOperator<Integer> xor = a -> a ^ 1; UnaryOperator<Integer> and = a -> a & 1; Function<Integer, Integer> compose = xor.andThen(and); System.out.println(compose.apply(2)); } } Output: 1 3.compose() Java import java.util.function.Function; import java.util.function.UnaryOperator; public class GFG { public static void main(String args[]) { UnaryOperator<Integer> xor = a -> a ^ 1; UnaryOperator<Integer> and = a -> a & 1; Function<Integer, Integer> compose = xor.compose(and); System.out.println(compose.apply(231)); } } Output: 0 Comment More infoAdvertise with us Next Article UnaryOperator Interface in Java P psil123 Follow Improve Article Tags : Misc Java Java - util package java-basics java-interfaces Java 8 Java-Functional Programming +3 More Practice Tags : JavaMisc Similar Reads IntUnaryOperator Interface in Java The IntUnaryOperator Interface 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 that takes in one argument and operates on it. Both its argument and return type are of the int data type. It is v 3 min read Marker Interface in Java In Java, a marker Interface is an empty interface that has no fields or methods. It is used just to mark or tag a class to tell Java or other programs something special about that class. These interfaces do not have any methods inside but act as metadata to provide information about the class. Examp 4 min read Nested Interface in Java In Java, we can declare interfaces as members of a class or another interface. Such an interface is called a member interface or nested interface. Interfaces declared outside any class can have only public and default (package-private) access specifiers. In Java, nested interfaces (interfaces declar 5 min read Java Runnable Interface java.lang.Runnable is an interface that is to be implemented by a class whose instances are intended to be executed by a thread. There are two ways to start a new Thread - Subclass Thread and implement Runnable. There is no need to subclass a Thread when a task can be done by overriding only the run 3 min read Serializable Interface in Java The Serializable interface is present in java.io package. It is a marker interface. A Marker Interface does not have any methods and fields. Thus classes implementing it do not have to implement any methods. Classes implement it if they want their instances to be Serialized or Deserialized. Serializ 2 min read Private Methods in Java 9 Interfaces Java 9 onwards, you can include private methods in interfaces. Before Java 9 it was not possible. Interfaces till Java 7 In Java SE 7 or earlier versions, an interface can have only two things i.e. Constant variables and Abstract methods. These interface methods MUST be implemented by classes which 4 min read Java Interface Methods In Java, an interface is an abstract type used to specify the behaviour of a class. One of the most important rules when working with interfaces is understanding how methods are declared and implemented. In this article, we are going to learn the interface methods in detail.Note: Interface methods a 3 min read TransferQueue Interface in Java The TransferQueue interface is a member of the Java Collections Framework. It was introduced in JDK 1.7, it belongs to java.util.concurrent package. The TransferQueue is a BlockingQueue in which a sending thread(producer) may wait for the receiving thread(consumers) to receive elements. TransferQueu 8 min read Match Lambdas to Interfaces in Java One of the most popular and important topics is lambda expression in java but before going straight into our discussion, let us have insight into some important things. Starting off with the interfaces in java as interfaces are the reference types that are similar to classes but containing only abst 5 min read Iterator Interface In Java Java Iterator Interface of java collections allows us to access elements of the collection and is used to iterate over the elements in the collection(Map, List or Set). It helps to easily retrieve the elements of a collection and perform operations on each element. Iterator is a universal iterator a 6 min read Like