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:
Instance Profile Credentials using Spring Cloud
Why String is Immutable in Java?
Java Program to Perform Arithmetic Operations on Numbers of Size
Java Program to Implement the linear congruential generator for Pseudo Random Number Generation
Java Program to Implement Ternary Heap
Guide to the Java ArrayList
Guide to Dynamic Tests in Junit 5
The Difference Between Collection.stream().forEach() and Collection.forEach()
HashMap trong Java hoạt động như thế nào?
Java Program to Implement Sorted Vector
Java Program to Check for balanced parenthesis by using Stacks
Reversing a Linked List in Java
Java Program to Implement Binomial Heap
Spring Cloud Bus
Validations for Enum Types
How to Store Duplicate Keys in a Map in Java?
Spring MVC and the @ModelAttribute Annotation
Java Program to Implement Ford–Fulkerson Algorithm
Java Program to Give an Implementation of the Traditional Chinese Postman Problem
Guide to the Volatile Keyword in Java
Java Program to Implement Cartesian Tree
Merging Streams in Java
Java Program to Check if a Given Graph Contain Hamiltonian Cycle or Not
Introduction to Eclipse Collections
Java Program to Implement Dijkstra’s Algorithm using Set
Java Program to Implement Hash Tables with Linear Probing
Java Program to Compute the Area of a Triangle Using Determinants
Custom Error Pages with Spring MVC
Lớp LinkedHashMap trong Java
So sánh Array và ArrayList trong Java
Control Structures in Java
Java Program to Solve the 0-1 Knapsack Problem