LongUnaryOperator Interface in Java
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 funciton 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)); } } |
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)); } } |
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)); } } |
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); } } } |
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)); } } |
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); } } } |
Exception: java.lang.NullPointerException
Recommended Posts:
- LongStream map(LongUnaryOperator mapper) in Java
- Java.util.function.BiPredicate interface in Java with Examples
- Java.util.function.IntPredicate interface in Java with Examples
- Java.util.function.DoublePredicate interface in Java with Examples
- Java.util.function.LongPredicate interface in Java with Examples
- Map Interface in Java
- Java 8 | IntToDoubleFunction Interface in Java with Examples
- Java 8 | IntToLongFunction Interface in Java with Examples
- Java 8 | DoubleToLongFunction Interface in Java with Examples
- Java 8 | Consumer Interface in Java with Examples
- Java 8 | BiConsumer Interface in Java with Examples
- UnaryOperator Interface in Java
- Runnable interface in Java
- ConcurrentMap Interface in java
- BlockingQueue Interface in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.



