A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/unaryoperator-interface-in-java/ below:

UnaryOperator Interface in Java - GeeksforGeeks

UnaryOperator Interface in Java

Last Updated : 11 Jul, 2025

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

Hence the UnaryOperator<T> overloads the Function<T, T> type. So it inherits the following methods from the Function Interface:

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));
    }
}
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));
    }
}
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));
    }
}
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));
    }
}


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4