We would like to know how to create new object with Supplier.
import java.util.Objects; import java.util.function.Supplier; // ww w .ja v a2s. c o m public class Main { public static My produce(Supplier<My> supp) { return supp.get(); } public static void main(String[] args) { My p1 = produce(() -> new My()); My p2 = produce(() -> new My()); System.out.println("Check the same object? " + Objects.equals(p1, p2)); } } class My { public My() { System.out.println("Sun Power initialized.."); } }
The code above generates the following result.