🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
In Java, the Supplier interface is a functional interface that represents a supplier of results. It is part of the java.util.function package and is commonly used to generate or supply values without taking any input.
Table of Contents
- What is
Supplier? - Methods and Syntax
- Examples of
Supplier - Real-World Use Case
- Conclusion
1. What is Supplier?
Supplier is a functional interface that provides a result when called. It is useful in scenarios where a value needs to be generated or fetched on demand without any input parameters.
2. Methods and Syntax
The main method in the Supplier interface is:
T get(): Gets a result.
Syntax
Supplier<T> supplier = () -> {
// logic to return a value
return result;
};
3. Examples of Supplier
Example 1: Returning a Constant String
import java.util.function.Supplier;
public class ConstantStringSupplier {
public static void main(String[] args) {
// Define a Supplier that returns a constant string
Supplier<String> stringSupplier = () -> "Hello, World!";
String result = stringSupplier.get();
System.out.println(result);
}
}
Output:
Hello, World!
Example 2: Generating a Random Number
import java.util.function.Supplier;
import java.util.Random;
public class RandomNumberSupplier {
public static void main(String[] args) {
// Define a Supplier that returns a random number
Supplier<Integer> randomSupplier = () -> new Random().nextInt(100);
int result = randomSupplier.get();
System.out.println("Random Number: " + result);
}
}
Output:
Random Number: [Random number between 0 and 99]
4. Real-World Use Case: Fetching Current Time
In applications, Supplier can be used to fetch the current system time.
import java.util.function.Supplier;
import java.time.LocalTime;
public class CurrentTimeSupplier {
public static void main(String[] args) {
// Define a Supplier to fetch the current time
Supplier<LocalTime> timeSupplier = LocalTime::now;
LocalTime currentTime = timeSupplier.get();
System.out.println("Current Time: " + currentTime);
}
}
Output:
Current Time: [Current time in HH:MM:SS format]
Conclusion
The Supplier interface is a versatile tool in Java for providing values without any input parameters. It is particularly beneficial in scenarios like generating constant values, random numbers, or fetching current data. Using Supplier can help create modular and reusable code, especially in functional programming contexts.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment