Lambda Expression - Function
原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/12807296.html
Function
Function是从T到R的一元函数映射,将参数T传递给一个函数,返回R,即 R = Function(T)
Demo
FunctionTest.java
1 package org.fool.java8; 2 3 import java.util.function.BiFunction; 4 import java.util.function.Function; 5 import java.util.function.IntFunction; 6 7 public class FunctionTest { 8 private static class Person { 9 private String name; 10 private Integer age; 11 12 public Person(String name, Integer age) { 13 this.name = name; 14 this.age = age; 15 } 16 17 public String getName() { 18 return name; 19 } 20 21 public void setName(String name) { 22 this.name = name; 23 } 24 25 public Integer getAge() { 26 return age; 27 } 28 29 public void setAge(Integer age) { 30 this.age = age; 31 } 32 33 @Override 34 public String toString() { 35 return "Person{" + 36 "name='" + name + '\'' + 37 ", age=" + age + 38 '}'; 39 } 40 } 41 42 private static String function(Person person, Function<Person, String> function) { 43 return function.apply(person); 44 } 45 46 private static double intFunction(int i, IntFunction<Double> function) { 47 return function.apply(i); 48 } 49 50 private static Person biFunction(String name, Integer age, BiFunction<String, Integer, Person> function) { 51 return function.apply(name, age); 52 } 53 54 public static void main(String[] args) { 55 String result1 = function(new Person("zhangsan", 18), Person::getName); 56 System.out.println(result1); 57 58 double result2 = intFunction(10, i -> i * 100.0d); 59 System.out.println(result2); 60 61 Person result3 = biFunction("wangwu", 38, Person::new); 62 System.out.println(result3); 63 } 64 }
Console Output
zhangsan 1000.0 Person{name='wangwu', age=38}
强者自救 圣者渡人