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:
Concrete Class in Java
Using Custom Banners in Spring Boot
Adding a Newline Character to a String in Java
Tránh lỗi NullPointerException trong Java như thế nào?
Converting a Stack Trace to a String in Java
Java Stream Filter with Lambda Expression
Java Program to Find Minimum Element in an Array using Linear Search
Injecting Prototype Beans into a Singleton Instance in Spring
Java Program to find the maximum subarray sum O(n^2) time(naive method)
Hướng dẫn Java Design Pattern – Null Object
The Difference Between Collection.stream().forEach() and Collection.forEach()
Introduction to Spring Cloud Rest Client with Netflix Ribbon
Check If a String Is Numeric in Java
Java Program to Implement Bloom Filter
Java Program to Implement Graham Scan Algorithm to Find the Convex Hull
Send email with authentication
Java Program to Implement Sieve Of Sundaram
Java Program to Implement K Way Merge Algorithm
Java Program to Check if an UnDirected Graph is a Tree or Not Using DFS
Spring RequestMapping
Java Program to Implement Lloyd’s Algorithm
Debugging Reactive Streams in Java
Guide to the ConcurrentSkipListMap
New Features in Java 13
Hướng dẫn Java Design Pattern – Observer
Java Program to Implement Weight Balanced Tree
Lớp Collectors trong Java 8
Validate email address exists or not by Java Code
Java Program to Construct an Expression Tree for an Postfix Expression
Generating Random Dates in Java
Java Program to Implement Sorting of Less than 100 Numbers in O(n) Complexity
An Intro to Spring Cloud Task