The Wayback Machine - http://web.archive.org/web/20241001021426/https://www.geeksforgeeks.org/longunaryoperator-interface-in-java/
Open In App

LongUnaryOperator Interface in Java

Last Updated : 18 Nov, 2019
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The LongUnaryOperator 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 which takes in one argument and operates on it. Both its argument and return type are of long data type.It is very similar to using an object of type UnaryOperator<Long>.

The lambda expression assigned to an object of UnaryOperator type is used to define its applyAsLong() which eventually applies the given operation on its argument.

Functions in LongUnaryOperator Interface

The LongUnaryOperator interface consists of the following functions:

1. identity()

This method returns a LongUnaryOperator which takes in one long value and returns it. The returned LongUnaryOperator does not perform any operation on its only value.

Syntax:

static  LongUnaryOperator identity()

Parameters: This method does not take in any parameter

Returns: A LongUnaryOperator which takes in one value and returns it.

Below is the code to illustrate identity() method:




import java.util.function.LongUnaryOperator;
  
public class GFG {
    public static void main(String args[])
    {
        LongUnaryOperator
            op
            = LongUnaryOperator.identity();
  
        System.out.println(op.applyAsLong(12));
    }
}


Output:

12

2. applyAsLong()

This method takes in one long value, performs the given operation and returns a long-valued result.

Syntax:

long applyAsLong(long operand)

Parameters: This method takes in one long valued parameter

Returns:: It returns a long valued result.

Below is the code to illustrate applyAsLong() method:

Program




import java.util.function.LongUnaryOperator;
  
public class GFG {
    public static void main(String args[])
    {
  
        LongUnaryOperator
            op
            = a -> 2 * a;
  
        System.out.println(op.applyAsLong(12));
    }
}


Output:

24

3. addThen()

It returns a composed LongUnaryOperator wherein the parameterized operator will be executed after the first one. If either operation throws an error, it is relayed to the caller of the composed operation.

Syntax:

default LongUnaryOperator andThen(LongUnaryOperator after)

Parameters: This method accepts a parameter after which is the operation to be applied after the current one.

Return Value: This method returns a composed LongUnaryOperator that applies the current operation first and then the after operation.

Exception: This method throws NullPointerException if the after operation is null.

Below is the code to illustrate addThen() method:

Program 1:




import java.util.function.LongUnaryOperator;
  
public class GFG {
    public static void main(String args[])
    {
  
        LongUnaryOperator op = a -> 2 * a;
  
        op = op.andThen(a -> 3 * a);
  
        System.out.println(op.applyAsLong(12));
    }
}


Output:

72

Program 2: To demonstrate when NullPointerException is returned.




import java.util.function.LongUnaryOperator;
  
public class GFG {
    public static void main(String args[])
    {
  
        try {
  
            LongUnaryOperator op = a -> 2 * a;
  
            op = op.andThen(null);
  
            System.out.println(op.applyAsLong(12));
        }
        catch (Exception e) {
  
            System.out.println("Exception: " + e);
        }
    }
}


Output:

Exception: java.lang.NullPointerException

4. compose()

It returns a composed LongUnaryOperator wherein the parameterized operation will be executed first and then the first one. If either operation throws an error, it is relayed to the caller of the composed operation.

Syntax:

default LongUnaryOperator compose(LongUnaryOperator before)

Parameters: This method accepts a parameter before which is the operation to be applied first and then the current one

Return Value: This method returns a composed LongUnaryOperator that applies the current operator after the parameterized operator

Exception: This method throws NullPointerException if the before operation is null.

Below is the code to illustrate compose() method:

Program 1:




import java.util.function.LongUnaryOperator;
  
public class GFG {
    public static void main(String args[])
    {
  
        LongUnaryOperator op = a -> a / 3;
  
        op = op.compose(a -> a * 6);
  
        System.out.println(op.applyAsLong(12));
    }
}


Output:

24

Program 2: To demonstrate when NullPointerException is returned.




import java.util.function.LongUnaryOperator;
  
public class GFG {
    public static void main(String args[])
    {
  
        try {
  
            LongUnaryOperator op = a -> a / 3;
  
            op = op.compose(null);
  
            System.out.println(op.applyAsLong(12));
        }
        catch (Exception e) {
  
            System.out.println("Exception: " + e);
        }
    }
}


Output:

Exception: java.lang.NullPointerException


Similar Reads

Why to Use Comparator Interface Rather than Comparable Interface in Java?
In Java, the Comparable and Comparator interfaces are used to sort collections of objects based on certain criteria. The Comparable interface is used to define the natural ordering of an object, whereas the Comparator interface is used to define custom ordering criteria for an object. Here are some reasons why you might want to use the Comparator i
8 min read
Java 8 | DoubleToIntFunction Interface in Java with Example
The DoubleToIntFunction 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 which takes in a double-valued argument and gives an int-valued result. The lambda expression assigned to an object of DoubleToIntFunction type is used to define
1 min read
Java 8 | IntToDoubleFunction Interface in Java with Examples
The IntToDoubleFunction 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 which takes in an int-valued argument and gives a double-valued result. The lambda expression assigned to an object of IntToDoubleFunction type is used to define
1 min read
Java 8 | DoubleToLongFunction Interface in Java with Examples
The DoubleToLongFunction 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 which takes in a double-valued argument and gives an long-valued result. The lambda expression assigned to an object of DoubleToLongFunction type is used to defi
1 min read
Java.util.function.BiPredicate interface in Java with Examples
The BiPredicate<T, V> interface was introduced in JDK 8. This interface is packaged in java.util.function package. It operates on two objects and returns a predicate value based on that condition. It is a functional interface and thus can be used in lambda expression also. public interface BiPredicate<T, V> Methods: test(): This functio
2 min read
Java.util.function.DoublePredicate interface in Java with Examples
The DoublePredicate interface was introduced in JDK 8. This interface is packaged in java.util.function package. It operates on a Double object and returns a predicate value based on a condition. It is a functional interface and thus can be used in lambda expression also. public interface DoublePredicate Methods test(): This function evaluates a co
2 min read
Java.util.function.LongPredicate interface in Java with Examples
The LongPredicate interface was introduced in JDK 8. This interface is packaged in java.util.function package. It operates on a long value and returns a predicate value based on a condition. It is a functional interface and thus can be used in lambda expression also. public interface LongPredicate Methods test(): This function evaluates a condition
2 min read
Java.util.function.IntPredicate interface in Java with Examples
The IntPredicate interface was introduced in JDK 8. This interface is packaged in java.util.function package. It operates on an integer value and returns a predicate value based on a condition. It is a functional interface and thus can be used in lambda expression also. public interface IntPredicate Methods: test(): This function evaluates a condit
2 min read
java.net.FileNameMap Interface in Java
java.net.FileNameMap is a Java Interface. FileNameMap interface is a part of the java.net package. FileNameMap provides a mechanism to map between a file name and a MIME type string. We can use FileNameMap getContentTypeFor method to get the MIME type of the specified file. Syntax: public interface FileNameMapMethod of java.net.FileNameMap Interfac
2 min read
ConcurrentMap Interface in java
ConcurrentMap is an interface and it is a member of the Java Collections Framework, which is introduced in JDK 1.5 represents a Map that is capable of handling concurrent access to it without affecting the consistency of entries in a map. ConcurrentMap interface present in java.util.concurrent package. It provides some extra methods apart from what
9 min read
Enumeration Interface In Java
java.util.Enumeration interface is one of the predefined interfaces, whose object is used for retrieving the data from collections framework variable( like Stack, Vector, HashTable etc.) in a forward direction only and not in the backward direction. This interface has been superceded by an iterator. The Enumeration Interface defines the functions b
3 min read
Java | Implementing Iterator and Iterable Interface
Iterators are used in Collection framework in Java to retrieve elements one by one. For more details and introduction related to this, see this link. Why it is needed to implement Iterable interface? Every class that implements Iterable interface appropriately, can be used in the enhanced For loop (for-each loop). The need to implement the Iterator
4 min read
Runnable interface in Java
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 of subclassing a Thread when a task can be done by overriding only run() method of Runnable. Steps to create a new thread
3 min read
Static method in Interface in Java
Static Methods in Interface are those methods, which are defined in the interface with the keyword static. Unlike other methods in Interface, these static methods contain the complete definition of the function and since the definition is complete and the method is static, therefore these methods cannot be overridden or changed in the implementatio
2 min read
ToLongFunction Interface in Java with Examples
The ToLongFunction 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 which takes in an argument of type T and produces a long-valued result. This functional interface takes in only one generic, namely:- T: denotes the type of the input
1 min read
LongToIntFunction Interface in Java with Examples
The LongToIntFunction 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 which takes in a long-valued argument and gives an int-valued result. The lambda expression assigned to an object of DoubleToLongFunction type is used to define its
1 min read
ToIntFunction Interface in Java with Examples
The ToIntFunction 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 which takes in an argument of type T and produces an int-valued result. This functional interface takes in only one generic, namely:- T: denotes the type of the input a
1 min read
ToDoubleFunction Interface in Java with Examples
The ToDoubleFunction 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 which takes in an argument of type T and produces a double-valued result. This functional interface takes in only one generic, namely:- T: denotes the type of the in
1 min read
IntFunction Interface in Java with Examples
The IntFunction 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 which takes in an int-valued argument and produces a result of type R. This functional interface takes in only one generic, namely:- R: denotes the type of the output of
1 min read
LongFunction Interface in Java with Examples
The LongFunction 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 which takes in a long-valued argument and produces a result of type R. This functional interface takes in only one generic, namely:- R: denotes the type of the output of
1 min read
Supplier Interface in Java with Examples
The Supplier 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 which does not take in any argument but produces a value of type T. Hence this functional interface takes in only one generic namely:- T: denotes the type of the result The
1 min read
IntConsumer Interface in Java with Examples
The IntConsumer 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 which takes in one int-valued argument but does not return any value. The lambda expression assigned to an object of IntConsumer type is used to define its accept() which
3 min read
LongConsumer Interface in Java with Examples
The LongConsumer 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 which takes in one long-valued argument but does not return any value. The lambda expression assigned to an object of LongConsumer type is used to define its accept() wh
3 min read
Java 8 | LongSupplier Interface with Examples
The LongSupplier 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 which does not take in any argument but produces a long value. The lambda expression assigned to an object of LongSupplier type is used to define its getAsLong() which e
1 min read
Java 8 | IntSupplier Interface with Examples
The IntSupplier 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 which does not take any argument but produces an int value. The lambda expression assigned to an object of IntSupplier type is used to define its getAsInt() which eventua
1 min read
Java 8 | BooleanSupplier Interface with Examples
The BooleanSupplier 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 which does not take in any argument but produces a boolean value. The lambda expression assigned to an object of BooleanSupplier type is used to define its getAsBoole
1 min read
Java 8 | DoubleSupplier Interface with Examples
The DoubleSupplier 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 which does not take in any argument but produces a double value. The lambda expression assigned to an object of DoubleSupplier type is used to define its getAsDouble()
1 min read
Java 8 | ObjLongConsumer Interface with Example
The ObjLongConsumer 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 which takes in two arguments and produces a result. However these kind of functions don't return any value. Hence this functional interface takes in one generic namel
2 min read
Java 8 | ObjIntConsumer Interface with Example
The ObjIntConsumer 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 which takes in two arguments and produces a result. However these kind of functions don't return any value. Hence this functional interface takes in one generic namely
2 min read
Java 8 | ObjDoubleConsumer Interface with Example
The ObjDoubleConsumer 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 which takes in two arguments and produces a result. However these kind of functions don't return any value. Hence this functional interface takes in one generic nam
2 min read