Java-8-Supplier
2020-02-02 本文已影响0人
Cool_Pomelo
Java-8-Supplier
interface Supplier<T>
源码:
@FunctionalInterface
public interface Supplier<T> {
T get();
}
这个接口,只是为我们提供了一个创建好的对象,这也符合接口的语义的定义,提供者,提供一个对象,直接理解成一个创建对象的工厂,就可以了
简单例子
public class M1 {
public static void main(String[] args) {
Supplier<String> supplier1 = String::new;
System.out.println(supplier1.get());
System.out.println("---------------");
Supplier<List<Integer>> supplier2 = ()->
Create_Data.supply_Integers();
System.out.println(
supplier2.get()
);
System.out.println("---------------");
Supplier<List<Person>> supplier3 = ()->
Create_Data.supply_Persons();
System.out.println(
supplier3.get()
);
}
}
针对特定类型
DoubleSupplier IntSupplier BooleanSupplier LongSupplier
public class M2 {
public static void main(String[] args) {
DoubleSupplier doubleSupplier = ()->
454.252;
System.out.println(doubleSupplier.getAsDouble());
System.out.println("---------------------");
IntSupplier intSupplier = ()->
100;
System.out.println(intSupplier.getAsInt());
}
}