Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator
functional
longunaryoperator
interface

What is the LongUnaryOperator functional interface in Java?

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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));
}
}
Implementation of the applyAsLong() method

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.

RELATED TAGS

java
communitycreator
functional
longunaryoperator
interface

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring