在相当不耐烦地等待 Java 8 发布时,在阅读了 Brian Goetz 的精彩的“Lambda 状态”文章后,我注意到根本没有涵盖函数组合。
根据上面的文章,在 Java 8 中应该可以做到以下几点:
// having classes Address and Person
public class Address {
private String country;
public String getCountry() {
return country;
}
}
public class Person {
private Address address;
public Address getAddress() {
return address;
}
}
// we should be able to reference their methods like
Function<Person, Address> personToAddress = Person::getAddress;
Function<Address, String> addressToCountry = Address::getCountry;
现在,如果我想组合这两个函数来映射Person
到国家/地区,我该如何在 Java 8 中实现呢?