- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to implement LongSupplier using lambda and method reference in Java?
LongSupplier is a built-in functional interface from java.util.function package. This interface doesn’t expect any input but produces a long-valued output. Since LongSupplier is a functional interface, it can be used as an assignment target for lambda expression and method reference and contains only one abstract method: getAsLong().
Syntax
@FunctionalInterface public interface LongSupplier { long getAsLong(); }
Example of Lambda Expression
import java.util.function.LongSupplier; public class LongSupplierLambdaTest { public static void main(String args[]) { LongSupplier supplier = () -> { // lambda expression return 75; }; long result = supplier.getAsLong(); System.out.println(result); } }
Output
75
Example of method reference
import java.util.function.LongSupplier; public class LongSupplierMethodRefTest { public static void main(String[] args) { LongSupplier supplier = LongSupplierMethodRefTest::getValue; // method reference double result = supplier.getAsLong(); System.out.println(result); } static long getValue() { return 50; } }
Output
50.0
- Related Articles
- How to implement LongPredicate using lambda and method reference in Java?
- How to implement DoubleUnaryOperator using lambda and method reference in Java?
- How to implement DoublePredicate using lambda and method reference in Java?
- How to implement LongBinaryOperator using lambda and method reference in Java?
- How to implement LongFunction using lambda and method reference in Java?
- How to implement DoubleSupplier using lambda and method reference in Java?
- How to implement IntToDoubleFunction using lambda and method reference in Java?
- How to implement IntToLongFunction using lambda and method reference in Java?
- How to implement LongToIntFunction using lambda and method reference in Java?
- How to implement LongToDoubleFunction using lambda and method reference in Java?
- How to implement DoubleBinaryOperator using lambda and method reference in Java?
- How to implement IntConsumer using lambda and method reference in Java?\n
- How to implement ToIntFunction using lambda and method reference in Java?\n
- How to implement the IntPredicate interface using lambda and method reference in Java?
- How to implement an action listener using method reference in Java?

Advertisements