class User {
private String name;
private String pass;
private long money;
private long pee;
public User(String name, String pass, long money, long pee) {
this.name = name;
this.pass = pass;
this.money = money;
this.pee = pee;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public long getMoney() {
return money;
}
public void setMoney(long money) {
this.money = money;
}
public long getPee() {
return pee;
}
public void setPee(long pee) {
this.pee = pee;
}
}
User u1 = new User("lrj", "11", 10, 4);
User u2 = new User("ldh", "12", 20, 5);
User u3 = new User("lrj", "13", 30, 6);
User u4 = new User("ldh", "14", 40, 7);
List l1 = new ArrayList(){{
add(u1);add(u2);add(u3);add(u4);
}};
Map collect = l1.stream().collect(Collectors.toMap(k -> k.getName(),
v -> new long[]{v.money, v.pee}, (x, y) -> {
x[0] = x[0] + y[0];
x[1] = x[1] + y[1];
return x;
}));
这样可以吗?