- 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 use Supplier interface in lambda expression in Java?
A Supplier<T> interface is a pre-defined interface that represents the supplier of results. It is instantiated using lambda expression or method reference or default constructor. The functional method of a Supplier<T> interface is the get() method. This interface belongs to java.util.function package.
Syntax
@FunctionalInterface public interface Supplier<T>
In the below program, we can use the Supplier interface in a lambda expression. The get() method only returns a value and doesn't accept any argument, so a lambda expression having an empty argument part.
Example
import java.util.*; import java.util.function.*; public class SupplierTest { public static void main(String args[]) { Supplier<String> supplier1 = () -> "TutorialsPoint"; // lambda expression System.out.println(supplier1.get()); Supplier<Integer> supplier2 = () -> 7; // lambda expression System.out.println(supplier2.get()); Person p = new Person("Raja"); Person p1 = get(() -> p); Person p2 = get(() -> p); System.out.println(p1.equals(p2)); } public static Person get(Supplier<Person> supplier) { return supplier.get(); } } // Person class class Person { public Person(String name) { System.out.println(name); } }
Output
TutorialsPoint 7 Raja true
- Related Articles
- How to use BinaryOperator interface in lambda expression in Java?
- How to use UnaryOperator interface in lambda expression in Java?
- How to use FileFilter interface in lambda expression in Java?\n
- How to implement ObjLongConsumer interface using lambda expression in Java?
- How to implement Function interface with lambda expression in Java?\n
- How to implement the ObjIntConsumer interface using lambda expression in Java?
- How to implement the Runnable interface using lambda expression in Java?
- How to use BooleanSupplier in lambda expression in Java?
- How to use IntSupplier in lambda expression in Java?
- Importance of Predicate interface in lambda expression in Java?
- How to use Predicate and BiPredicate in lambda expression in Java?
- How to use a return statement in lambda expression in Java?
- How to use an ArrayList in lambda expression in Java?\n
- How to use Consumer and BiConsumer interfaces in lambda expression in Java?
- How to use Function and BiFunction interfaces in lambda expression in Java?

Advertisements