Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator
functional
longsupplier

What is the LongSupplier 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.

LongSupplier is a functional interface that produces results of type long without accepting any inputs.

The results produced each time can be the same or different.

The interface contains one method, that is, getAsLong.

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

import java.util.function.LongSupplier;

getAsLong() method

This is the functional method of the interface. This method is used to get the long values.

Syntax

long getAsLong()

Parameters

The method has no parameters.

Return value

The method returns the generated result of type long.

Code

import java.util.Random;
import java.util.function.*;
public class Main{
public static void main(String[] args) {
// LongSupplier interface implementation that generates random long value
LongSupplier randomSupplier = () -> new Random().nextLong();
int count = 5;
// Calling getAsLong method to get the random value
while(count-- > 0) System.out.println(randomSupplier.getAsLong());
}
}

Explanation

In the above code, we create an implementation of the LongSupplier interface that generates a random long value every time the method getAsLong() is invoked. The getAsLong() method is called five times to produce five random long values, using the while loop.

RELATED TAGS

java
communitycreator
functional
longsupplier

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