原创转载请注明出处:http://agilestyle.iteye.com/blog/2424770
Supplier
Supplier表示结果的供应者
e.g.
package org.fool.java8;
import java.util.function.Supplier;
public class SupplierTest {
private static class Person {
private String name;
private Integer age;
public Person(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
private static Person createPerson(Supplier<Person> supplier) {
return supplier.get();
}
public static void main(String[] args) {
Supplier<String> supplier = String::new;
System.out.println(supplier.get().getClass());
Person person = createPerson(() -> new Person("zhangsan", 18));
System.out.println(person);
}
}
Console Output
class java.lang.String Person{name='zhangsan', age=18}