我的一位同事向我提出了一个有趣的问题,我无法找到一个整洁而漂亮的Java 8解决方案.问题是流过POJO列表,然后根据多个属性在地图中收集它们 - 映射导致POJO多次出现
想象一下以下POJO:
private static class Customer {
public String first;
public String last;
public Customer(String first, String last) {
this.first = first;
this.last = last;
}
public String toString() {
return "Customer(" + first + " " + last + ")";
}
}
Run Code Online (Sandbox Code Playgroud)
将其设置为List<Customer>:
// The list of customers
List<Customer> customers = Arrays.asList(
new Customer("Johnny", "Puma"),
new Customer("Super", "Mac"));
Run Code Online (Sandbox Code Playgroud)
备选方案1:使用Map"流"外部(或更确切地说是外部forEach).
// Alt 1: not pretty since the resulting map is …Run Code Online (Sandbox Code Playgroud)