Supplier Interface is a part of the java.util.function package which is introduced in Java 8. It is an in-built Functional Interface. This function doesn’t expect any input but produces an output in the form of a value of type T. In this post, we are going to see several implementations of Supplier Interface by using different examples.
Supplier Interface in Java 8 Examples...!!! Click To Tweet
Look at Supplier Javadoc description below:
As you’ve seen in the above screenshot, Supplier Interface contains only the following function:
T get();
This signifies that this method doesn’t take any argument as an input but produces an output value of type T.
Several Implementations of Supplier Interface in Java 8...!!! Click To Tweet
Let’s begin:
Example 1. Supplier resulting a string value
1 2 3 4 5 6 7 8 9 10 11 12 13 | import java.util.function.Supplier; public class SupplierInterfaceJava8Example1 { public static void main(String args[]) { System.out.println("Ex. 1 - Java 8 Supplier Interface\n"); Supplier <String> simpleString = () -> "Hello Basic Supplier"; System.out.println(simpleString.get()); } } |
Example 2. Supplier resulting a HashMap
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import java.util.HashMap; import java.util.function.Supplier; public class SupplierInterfaceJava8Example2 { public static void main(String[] args) { System.out.println("Ex. 2 - Java 8 Supplier Interface\n"); HashMap <Integer, String> personHashMap = new HashMap <Integer, String> (); personHashMap.put(19, "Isha"); personHashMap.put(43, "Gaurangee"); personHashMap.put(4, "Yashika"); Supplier <HashMap <Integer, String>> supplierPersonObj = () -> personHashMap; printMap(supplierPersonObj); } public static void printMap(Supplier < HashMap < Integer, String >> supplierObj) { System.out.println(supplierObj.get()); } } |
Example 3. Supplier resulting an ArrayList using Stream API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | import java.util.ArrayList; import java.util.List; import java.util.function.Supplier; public class SupplierInterfaceJava8Example3 { public static void main(String[] args) { System.out.println("Ex. 3 - Java 8 Supplier Interface\n"); List <String> studentNames = new ArrayList <String> (); studentNames.add("Yogi"); studentNames.add("Yash"); studentNames.add("Yashika"); studentNames.add("Pulkit"); studentNames.add("Noni"); studentNames.add("Isha"); studentNames.add("DV"); studentNames.stream().forEach((item) -> { printName(() -> item); }); } public static void printName(Supplier <String> supplier) { System.out.println(supplier.get()); } } |
Example 4. Supplier resulting a custom Class object (like ‘Students’ in this case)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import java.util.function.Supplier; import org.personal.samples.Students; public class SupplierInterfaceJava8Example4 { public static void main(String[] args) { System.out.println("Ex. 4 - Java 8 Supplier Interface\n"); Supplier <Students> supplierStudentObj = () -> { return new Students(25, "Isha Durani"); }; System.out.println("Student details: \n" + "Name: " + supplierStudentObj.get().getName() + "\nAge: " + supplierStudentObj.get().getAge()); } } |
Students Class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | public class Students { int age; String name; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Students() {} public Students(int age, String name) { this.age = age; this.name = name; } } |
Java 8 Supplier Interface is an absolute useful addition as part of ‘Functional Interfaces’ and can serve variety of purposes. It is quite powerful as it can be used as a higher order functions through lambda functions and above examples could help you to get better idea on how to implement it.
Supplier Interface in Java 8 Examples...!!! Click To Tweet
Do you like this Post? – then check my other helpful posts:
- Passing Function as a Parameter in another Method in Java 8
- Collection sorting using Lambda in Java 8
- Generate Prime numbers in Java 8
Other Useful References: