Table of Contents
1. Giới thiệu Supplier<T>
Trong Java 8, Supplier<T> là một functional interface và do đó nó có thể được sử dụng với lambda expression hoặc method reference cho một mục đích cụ thể nào đó. Supplier<T> làm ngược lại với Consumer<T>, nó là một phương thức trừu tượng không tham số, và trả về một đối tượng bằng cách gọi phương thức get() của nó.
Mục đích chính của Interface này là cung cấp một cái gì đó cho chúng ta khi cần.
Interface Supplier được khai báo trong package java.util.function như sau:

Trong đó:
- T get() : là một phương thức trừu tượng có thể được sử dụng với lambda expression hoặc method reference cho một mục đích cụ thể nào đó.
- Phương thức get() sẽ return một giá trị cụ thể cho chúng ta.
2. Một số ví dụ
2.1. Tạo Supplier sử dụng Lambda Expression
package com.maixuanviet.supplier;
import java.util.function.Supplier;
public class SupplierExample1 {
public static void main(String[] args) {
Supplier<String> supplier = () -> "Welcome to maixuanviet.com";
String hello = supplier.get();
System.out.println(hello);
}
}
2.2. Tạo Supplier sử dụng Method Reference
package com.maixuanviet.supplier;
import java.util.function.Supplier;
class Programing {
public String language;
public int experience;
public Programing() {
this.language = "Java";
this.experience = 5;
}
public Programing(String language, int experience) {
this.language = language;
this.experience = experience;
}
public void print() {
System.out.println("Language: " + language + " - Experience: " + experience);
}
public static String getDefaulLanguage() {
return "Java";
}
}
public class SupplierExample2 {
public static void main(String[] args) {
Supplier<Programing> supplier1 = Programing::new;
Programing lang = supplier1.get();
lang.print();
Supplier<String> supplier2 = Programing::getDefaulLanguage;
String defaultLang = supplier2.get();
System.out.println("Default Language: " + defaultLang);
}
}
Output của chương trình trên:
Language: Java - Experience: 5 Default Language: Java
2.3. Sử dụng Supplier với các lớp cho kiểu dữ liệu nguyên thủy (primitive type)
Java 8 cung cấp một số Interface Supplier cho các wrapper class của kiểu dữ liệu nguyên thủy như sau:
- BooleanSupplier : là một functional interface có thể được sử dụng như một Supplier cho các giá trị boolean. BooleanSupplier luôn trả về giá trị boolean bằng phương thức getAsBoolean().
- IntSupplier : là một functional interface có thể được sử dụng như một Supplier cho các giá trị int. IntSupplier luôn trả về giá trị int bằng phương thức getAsInt().
- LongSupplier : là một functional interface có thể được sử dụng như một Supplier cho các giá trị long. LongSupplier luôn trả về giá trị long bằng phương thức getAsLong().
- DoubleSupplier : là một functional interface có thể được sử dụng như một Supplier cho các giá trị double. DoubleSupplier luôn trả về giá trị double bằng phương thức getAsDouble().
package com.maixuanviet.supplier;
import java.util.function.BooleanSupplier;
import java.util.function.DoubleSupplier;
import java.util.function.IntSupplier;
import java.util.function.LongSupplier;
class NumberUtils {
public static boolean getBooleanValue() {
return true;
}
public static int getIntValue() {
return 1;
}
public static long getLongValue() {
return 2;
}
public static double getDoubleValue() {
return 3;
}
}
public class SupplierExample3 {
public static void main(String[] args) {
BooleanSupplier bs = NumberUtils::getBooleanValue;
System.out.println("Boolean Value: " + bs.getAsBoolean());
IntSupplier dbs = NumberUtils::getIntValue;
System.out.println("Integer Value: " + dbs.getAsInt());
LongSupplier ls = NumberUtils::getLongValue;
System.out.println("Long Value: " + ls.getAsLong());
DoubleSupplier ds = NumberUtils::getDoubleValue;
System.out.println("Double Value: " + ds.getAsDouble());
}
}
Output của chương trình trên:
Boolean Value: true Integer Value: 1 Long Value: 2 Double Value: 3.0
Related posts:
Java Program to Check whether Graph is Biconnected
Rest Web service: Filter và Interceptor với Jersey 2.x (P1)
Java 8 Predicate Chain
Spring Boot Security Auto-Configuration
Java Program to Implement Fisher-Yates Algorithm for Array Shuffling
How to Return 404 with Spring WebFlux
Service Registration with Eureka
Spring Security OAuth Login with WebFlux
Vấn đề Nhà sản xuất (Producer) – Người tiêu dùng (Consumer) và đồng bộ hóa các luồng trong Java
New in Spring Security OAuth2 – Verify Claims
Spring Web Annotations
Introduction to Apache Commons Text
Configuring a DataSource Programmatically in Spring Boot
Java Program to Implement Flood Fill Algorithm
Java 8 Collectors toMap
Java Program to Implement CountMinSketch
Java Program to Implement the Hungarian Algorithm for Bipartite Matching
The SpringJUnitConfig and SpringJUnitWebConfig Annotations in Spring 5
Java Program to Perform Insertion in a 2 Dimension K-D Tree
A Guide to Queries in Spring Data MongoDB
Guide to Spring @Autowired
Converting Between Byte Arrays and Hexadecimal Strings in Java
Java Program to do a Depth First Search/Traversal on a graph non-recursively
Guide to WeakHashMap in Java
Java Stream Filter with Lambda Expression
Constructor Injection in Spring with Lombok
Validate email address exists or not by Java Code
Từ khóa throw và throws trong Java
Spring REST with a Zuul Proxy
Spring Boot - Admin Server
Java Program to Implement PrinterStateReasons API
Java Program to Implement Expression Tree