What is the LongUnaryOperator functional interface in Java?

Overview


LongUnaryOperator is a functional interface that represents an operation on a single long-valued argument and produces a long-valued result. The applyAsLong() method is the functional method of the interface.

The LongUnaryOperator interface is defined in the java.util.function package. To import the LongUnaryOperator interface, check the following import statement.

import java.util.function.LongUnaryOperator;

Syntax


long applyAsLong(long operand);

Parameters

  • long operand: This is the long-valued operand.

Return value

This method returns a long-valued result.

Code


import java.util.function.LongUnaryOperator;
public class Main {
public static void main(String[] args){
LongUnaryOperator squareOfNumFunction = num -> num * num;
long arg = 543;
System.out.println("Result of applyAsLong() on squareOfNumFunction --> " + squareOfNumFunction.applyAsLong(arg));
}
}

Explanation


  • Line 1: We import the LongUnaryOperator interface from the function package.
  • Line 6: We define an implementation of the LongUnaryOperator interface called squareOfNumFunction. squareOfNumFunction accepts a long argument and returns the result square of the given argument.
  • Line 7: We define a long value called arg.
  • Line 8: We print the result of using the applyAsLong() method and pass arg as an argument.

Free Resources