In Java 8, Supplier is a functional interface; it takes no arguments and returns a result.
Supplier.java
@FunctionalInterface public interface Supplier<T> { T get(); }
1. Supplier
1.1 This example uses Supplier
to return a current date-time.
Java8Supplier1.java
package com.favtuts.java8; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.function.Supplier; public class Java8Supplier1 { private static final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); public static void main(String[] args) { Supplier<LocalDateTime> s = () -> LocalDateTime.now(); LocalDateTime time = s.get(); System.out.println(time); Supplier<String> s1 = () -> dtf.format(LocalDateTime.now()); String time2 = s1.get(); System.out.println(time2); } }
Output
2022-05-25T13:39:47.113689
2022-05-25 13:39:47
2. Returns a Supplier
Java8Supplier2.java
package com.favtuts.java8; import java.util.ArrayList; import java.util.List; import java.util.function.Supplier; public class Java8Supplier2<T> { public static void main(String[] args) { Java8Supplier2<String> obj = new Java8Supplier2(); List<String> list = obj.supplier().get(); } public Supplier<List<T>> supplier() { // lambda // return () -> new ArrayList<>(); // constructor reference return ArrayList::new; } }
3. Factory
3.1 A simple factory method to return a Developer
object.
Java8Supplier3.java
package com.favtuts.java8; import java.math.BigDecimal; import java.time.LocalDate; import java.util.function.Supplier; public class Java8Supplier3 { public static void main(String[] args) { Developer obj = factory(Developer::new); System.out.println(obj); Developer obj2 = factory(() -> new Developer("favtuts")); System.out.println(obj2); } public static Developer factory(Supplier<? extends Developer> s) { Developer developer = s.get(); if (developer.getName() == null || "".equals(developer.getName())) { developer.setName("default"); } developer.setSalary(BigDecimal.ONE); developer.setStart(LocalDate.of(2017, 8, 8)); return developer; } }
Developer.java
package com.favtuts.java8; import java.math.BigDecimal; import java.time.LocalDate; public class Developer { String name; BigDecimal salary; LocalDate start; // for factory(Developer::new); public Developer() { } // for factory(() -> new Developer("mkyong")); public Developer(String name) { this.name = name; } // get, set, constructor, toString //... public String getName() { return this.name; } public void setName(String name) { this.name = name; } public BigDecimal getSalary() { return this.salary; } public void setSalary(BigDecimal salary) { this.salary = salary; } public LocalDate getStart() { return this.start; } public void setStart(LocalDate start) { this.start = start; } @Override public String toString() { return "Developer{" + " name='" + getName() + "'" + ", salary='" + getSalary() + "'" + ", start='" + getStart() + "'" + "}"; } }
Output
Developer{name='default', salary=1, start=2017-08-08}
Developer{name='favtuts', salary=1, start=2017-08-08}