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:
Function trong Java 8
Spring Boot - Hystrix
New Features in Java 11
Java Program to Compute Discrete Fourier Transform Using the Fast Fourier Transform Approach
Java Program to Implement Sieve Of Atkin
Java Program to Find the Median of two Sorted Arrays using Binary Search Approach
Spring Cloud AWS – RDS
Cài đặt và sử dụng Swagger UI
Mapping a Dynamic JSON Object with Jackson
A Guide to Concurrent Queues in Java
Spring WebClient vs. RestTemplate
Giới thiệu Java Service Provider Interface (SPI) – Tạo các ứng dụng Java dễ mở rộng
Inheritance and Composition (Is-a vs Has-a relationship) in Java
Java Convenience Factory Methods for Collections
Practical Java Examples of the Big O Notation
So sánh HashMap và Hashtable trong Java
An Example of Load Balancing with Zuul and Eureka
Java Program to Perform Finite State Automaton based Search
Most commonly used String methods in Java
Iterating over Enum Values in Java
Java Program to Implement ConcurrentLinkedQueue API
Java Program to Generate Randomized Sequence of Given Range of Numbers
Tính đóng gói (Encapsulation) trong java
A Guide to Java HashMap
XML-Based Injection in Spring
How to use the Spring FactoryBean?
Redirect to Different Pages after Login with Spring Security
Immutable Objects in Java
Java Program to Implement Fibonacci Heap
Java 8 Collectors toMap
RestTemplate Post Request with JSON
Guide To CompletableFuture