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:
Check If Two Lists are Equal in Java
Object cloning trong java
Spring Cloud – Bootstrapping
Functional Interfaces in Java 8
Giới thiệu Google Guice – Injection, Scope
Spring Boot - Thymeleaf
New Stream Collectors in Java 9
Java Program to Perform the Unique Factorization of a Given Number
Java Program to Implement an Algorithm to Find the Global min Cut in a Graph
Java Program to Implement Hopcroft Algorithm
Java Program to Implement Attribute API
Sử dụng CountDownLatch trong Java
Quick Guide on Loading Initial Data with Spring Boot
Java Program to Find Shortest Path Between All Vertices Using Floyd-Warshall’s Algorithm
Java Program to Implement Circular Doubly Linked List
Generating Random Dates in Java
Returning Custom Status Codes from Spring Controllers
What is Thread-Safety and How to Achieve it?
Hướng dẫn Java Design Pattern – Decorator
Registration – Password Strength and Rules
Java Program to Perform Uniform Binary Search
Java Program to Implement Disjoint Set Data Structure
Custom Exception trong Java
Multi Dimensional ArrayList in Java
Reactive WebSockets with Spring 5
Finding Max/Min of a List or Collection
Sao chép các phần tử của một mảng sang mảng khác như thế nào?
Getting a File’s Mime Type in Java
Spring Security Logout
Filtering and Transforming Collections in Guava
Java 8 StringJoiner
Biểu thức Lambda trong Java 8 – Lambda Expressions