最近在做一个大的项目,其中有一个通过List里面对象的属性值来去重List,并把相同属性的其他属性相加,都说通过Map的key唯一,来去重。记录一下
首先有一个实体类User
package com.fmj.test;
public class User {
private String id;
private String name;
private String password;
private int age;
private int jine;
private String desc;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getJine() {
return jine;
}
public void setJine(int jine) {
this.jine = jine;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public User(String id, String name, String password, int age, int jine, String desc) {
this.jine = jine;
this.age = age;
this.password = password;
this.id = id;
this.name = name;
this.desc = desc;
}
public User(String id, String name) {
this.id = id;
this.name = name;
}
public User() {
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", password=" + password + ", age=" + age + ", jine=" + jine
+ ", desc=" + desc + "]";
}
}
我们想 通过 id 和name来去重,并把age 和jine 相加起来
private static void sub() {
List<User> userList = new ArrayList<User>();
User u1 = new User("1", "zhangyi", "zhangyipwd", 21, 101, "yiban1");
User u2 = new User("2", "zhanger", "zhangerpwd", 22, 102, "yiban2");
User u3 = new User("1", "zhangyi", "zhangyipwd", 23, 103, "yiban3");
User u4 = new User("4", "zhansi", "zhangsipwd", 24, 104, "yiban4");
User u5 = new User("5", "zhangwu", "zhangwupwd", 25, 105, "yiban5");
User u6 = new User("6", "zhangliu", "zhangliupwd", 26, 106, "yiban6");
userList.add(u1);
userList.add(u2);
userList.add(u3);
userList.add(u4);
userList.add(u5);
userList.add(u6);
List<User> newuserList = new ArrayList<User>();
userList.parallelStream().collect(Collectors.groupingBy(o -> (o.getId() + o.getName()), Collectors.toList())).forEach(
(id, transfer) -> {
transfer.stream().reduce((a, b) -> new User(a.getId(), a.getName(), a.getPassword(), a.getAge() + b.getAge(), a.getJine() + b.getJine(), a.getDesc())).ifPresent(newuserList:: add);
});
System.out.println("处理过的List:--");
newuserList.forEach(item->{
System.out.println(item.toString());
});
}
我上面的例子,可以看出u1 和 u3 会被合并,变成一条,运行结果如下
处理过的List:--
User [id=2, name=zhanger, password=zhangerpwd, age=22, jine=102, desc=yiban2]
User [id=6, name=zhangliu, password=zhangliupwd, age=26, jine=106, desc=yiban6]
User [id=5, name=zhangwu, password=zhangwupwd, age=25, jine=105, desc=yiban5]
User [id=4, name=zhansi, password=zhangsipwd, age=24, jine=104, desc=yiban4]
User [id=1, name=zhangyi, password=zhangyipwd, age=44, jine=204, desc=yiban3]
这就实现了去重累加,这里我复制了,https://www.cnblogs.com/edda/p/14191917.html,这个文章,对这个tream 确实,小有难度
还记得我上面说过的用Map来处理吗?下面这个例子会容易理解一点
public static List<User> sub1(List<User> list) {
Map<String, User> map = new HashMap<String, User>();
list.stream()
.forEach(user -> {
User last = map.get(user.getId()+user.getName());
if(map.containsKey(user.getId()+user.getName())){
user.setAge(user.getAge() + last.getAge());
user.setJine(user.getJine() + last.getJine());
map.put(user.getId()+user.getName(), user );
}else{
map.put(user.getId()+user.getName(), user);
}
});
return map.values().stream().collect(Collectors.toList());
}
使用
private static void sub() {
List<User> userList = new ArrayList<User>();
User u1 = new User("1", "zhangyi", "zhangyipwd", 21, 101, "yiban1");
User u2 = new User("2", "zhanger", "zhangerpwd", 22, 102, "yiban2");
User u3 = new User("1", "zhangyi", "zhangyipwd", 23, 103, "yiban3");
User u4 = new User("4", "zhansi", "zhangsipwd", 24, 104, "yiban4");
User u5 = new User("5", "zhangwu", "zhangwupwd", 25, 105, "yiban5");
User u6 = new User("6", "zhangliu", "zhangliupwd", 26, 106, "yiban6");
userList.add(u1);
userList.add(u2);
userList.add(u3);
userList.add(u4);
userList.add(u5);
userList.add(u6);
List<User> newuserList1 = sub1(userList);
System.out.println("处理过的List:--");
newuserList1.forEach(item->{
System.out.println(item.toString());
});
}
结果如下:
处理过的List:--
User [id=2, name=zhanger, password=zhangerpwd, age=22, jine=102, desc=yiban2]
User [id=6, name=zhangliu, password=zhangliupwd, age=26, jine=106, desc=yiban6]
User [id=5, name=zhangwu, password=zhangwupwd, age=25, jine=105, desc=yiban5]
User [id=1, name=zhangyi, password=zhangyipwd, age=44, jine=204, desc=yiban3]
User [id=4, name=zhansi, password=zhangsipwd, age=24, jine=104, desc=yiban4]
加上 这一句 排序
Collections.sort(newuserList1, (o1, o2)-> Integer.parseInt(o1.getId()) - Integer.parseInt(o2.getId()));
结果如下
处理过的List:--
User [id=1, name=zhangyi, password=zhangyipwd, age=44, jine=204, desc=yiban3]
User [id=2, name=zhanger, password=zhangerpwd, age=22, jine=102, desc=yiban2]
User [id=4, name=zhansi, password=zhangsipwd, age=24, jine=104, desc=yiban4]
User [id=5, name=zhangwu, password=zhangwupwd, age=25, jine=105, desc=yiban5]
User [id=6, name=zhangliu, password=zhangliupwd, age=26, jine=106, desc=yiban6]