The Supplier is a class that was added in Java 8 in the java.util package with is a functional inference, that takes in no input and returns a value.
Supplier.java@FunctionalInterface
public interface Supplier<T> {
/**
* Gets a result.
*
* @return a result
*/
T get();
}
Let's take a look at some examples:
Example 1: Hello World!
import java.util.function.Supplier;
public class SupplierHelloWorld {
public static void main(String[] args) {
Supplier<String> supplier = () -> "Hello, world!";
String result = supplier.get();
System.out.println(result);
}
}
Example 2: Random Number Supplier
import java.util.Random;
import java.util.function.Supplier;
public class RandomNumberSupplier {
public static void main(String[] args) {
Supplier<Integer> randomNumberSupplier = () -> new Random().nextInt(500);
int randNo = randomNumberSupplier.get();
System.out.println("Random Number: " + randNo);
}
}
Example 3: Current Date Supplier
import java.time.LocalDateTime;
import java.util.function.Supplier;
public class DateTimeSupplier {
public static void main(String[] args) {
Supplier<LocalDateTime> dateTimeSupplier = LocalDateTime::now;
LocalDateTime currentDateTime = dateTimeSupplier.get();
System.out.println("Current Date and Time: " + currentDateTime);
}
}
-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- Convert Java Map Collection Object to JSON String using Jackson
- Java Stream flatmap() Examples
- [Fix] Instant java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Years
- How to run Java Unit Test cases with Apache Maven?
- How to check if Java main thread is alive
- [Fix] java: incompatible types: incompatible parameter types in lambda expression error
- Parsing CSV file using Java code example (Comma Separated File)
- Unhandled exception type InterruptedException : Java Threads
- Native getClass() method from java.lang.Object Class Explained with examples.
- Java Jackson ObjectMapper Class with Examples
- Java 8 Streams map() with examples
- Java 8 - Convert List to Map Examples
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- Java Stream with Multiple Filters Example
- How to Clear StringJoiner in Java 8
- Spring 5 IoC Example with application Context XML (ClassPathXmlApplicationContext) and Gradle.
- How to get end of line (EOL) or new line character \r \n in Java
- Spring Boot CRUD Examples using JDBCTemplate
- Delete a File in Java with Examples
- Implementing Insertion Sort Algorithm in Java Program
- Java JDBC Batch Update Example with PreparedStatement
- Java JDBC Select Multiple Records from table as List using PreparedStatement
- [Hibernate] The method buildSessionFactory() from the type Configuration is deprecated
- How to fix Java HTTP java.net.UnknownHostException
- Java 8 Display time in 12 hour AM PM format
More Posts:
- How to open a file via Mac Terminal - MacOS
- How to install PowerShell on macOS - Powershell
- 12 August - International Youth Day celebrated worldwide - News
- List of Special Parameters/Variables in Bash Shell Scripting - Linux
- Fix Error - Another active Homebrew update process is already in progress - MacOS
- Python: Read a file into a List - Python
- Google YouTube Gmail down, not working worldwide - 503, 502, something went wrong error - News
- Error: Unable to access jarfile jarFileName.jar file [Windows] - Java