Lambda Expression - Supplier
原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/12807635.html
Supplier
Supplier表示结果的供应者
Demo
SupplierTest.java
1 package org.fool.java8; 2 3 import java.util.function.Supplier; 4 5 public class SupplierTest { 6 private static class Person { 7 private String name; 8 private Integer age; 9 10 public Person(String name, Integer age) { 11 this.name = name; 12 this.age = age; 13 } 14 15 public String getName() { 16 return name; 17 } 18 19 public void setName(String name) { 20 this.name = name; 21 } 22 23 public Integer getAge() { 24 return age; 25 } 26 27 public void setAge(Integer age) { 28 this.age = age; 29 } 30 31 @Override 32 public String toString() { 33 return "Person{" + 34 "name='" + name + '\'' + 35 ", age=" + age + 36 '}'; 37 } 38 } 39 40 private static Person createPerson(Supplier<Person> supplier) { 41 return supplier.get(); 42 } 43 44 public static void main(String[] args) { 45 Supplier<String> supplier = String::new; 46 System.out.println(supplier.get().getClass()); 47 48 Person person = createPerson(() -> new Person("zhangsan", 18)); 49 System.out.println(person); 50 } 51 }
Console Output
class java.lang.String Person{name='zhangsan', age=18}
强者自救 圣者渡人