LongUnaryOperator Interface in Java 8 with examples

This entry is part of 43 in the series Java 8 Functional Interfaces

LongUnaryOperator Interface is a part of the java.util.function package which is introduced in Java 8. It is an in-built Functional Interface. This function expects an argument of type long as input but produces the output of same type as input argument. In this post, we are going to see several implementations of LongUnaryOperator Interface by using different examples.

LongUnaryOperator_Java8_Techndeck

LongUnaryOperator Interface in Java 8 with Examples...!!! Click To Tweet

Look at LongUnaryOperator Javadoc description below:

LongUnaryOperator_Java8_Signature_Techndeck

 

 

 

 

LongUnaryOperator Interface contains 4 methods:

  1. applyAsLong

  2. compose

  3. andThen

  4. identity

 

Let’s discuss these methods:

identity

This method returns a long unary operator that always returns its input argument.

applyAsLong

This method performs operation on the given argument and return the function result.

Compose

This method returns a composed operator that first applies the before operator to its input, and then applies this operator to the result. If evaluation of either operator throws an exception, it is relayed to the caller of the composed operator.

andThen

This method returns a composed operator that first applies this operator to its input, and then applies the after operator to the result. If evaluation of either operator throws an exception, it is relayed to the caller of the composed operator.

 

Below are the several examples to demonstrate above methods:

Example 1. ‘applyAsLong’

Output:

 

Example 2. ‘andThen’

Output:

 

Example 3. ‘compose’

Output:

 

Java 8 LongUnaryOperator Interface is an absolute useful addition as part of ‘Functional Interfaces’ and can serve variety of purposes. It is quite powerful as it can be used as a higher order functions through lambda functions and above examples could help you to get better idea on how to implement it.

 

LongUnaryOperator Interface in Java 8 with Examples...!!! Click To Tweet

 

Do you like this Post? – then check my other helpful posts:

Other Useful References:

Promotional Content:

Loading RSS Feed
Series NavigationUnaryOperator Interface in Java 8 with examples >>

Leave a Reply

Your email address will not be published.Required fields are marked *

Facebook Twitter Instagram YouTube