合并两个 list<map>, 并将 userId 相同的所有属性合并到一个 map 中
list1中对象的属性:userId、userName
list2中对象的属性:userId、gender、age
最终总集合中对象的属性:userId、userName、gender、age
public class Test3 {
public static void main(String[] args){
List<Map<String,Object>> list1 = new ArrayList<>();
Map<String,Object> data=new HashMap<>();
data.put("userId","100001");
data.put("userName","唐僧");
list1.add(data);
data=new HashMap<>();
data.put("userId","100002");
data.put("userName","八戒");
list1.add(data);
List<Map<String,Object>> list2 = new ArrayList<>();
data=new HashMap<>();
data.put("userId","100001");
data.put("gender","男");
data.put("age",20);
list2.add(data);
data=new HashMap<>();
data.put("userId","100002");
data.put("gender","雄");
data.put("age",1000);
list2.add(data);
System.out.println(list1);
System.out.println(list2);
//使用stream流把list1和list2根据属性userId合并一个list集合
list1.stream().map(m1 -> {
list2.stream().filter(m2 -> m1.get("userId").equals(m2.get("userId"))).forEach( m2 -> {
m1.put("gender", m2.get("gender"));
m1.put("age", m2.get("age"));
});
return m1;
}).collect(Collectors.toList());
System.out.println(list1);
System.out.println(list2);
}
}
结果可见,userId 相同的所有属性合并到集合 list1中。
合并两个 list<map>, 并将 userId 相同的某个属性值进行运算
list1中对象的属性:id、name、amount
list2中对象的属性:id、name、amount
最终总集合中对象的属性:id、name、amount(金额进行累加)
User :
@Data
@AllArgsConstructor
public class User {
public String id;
public String name;
public BigDecimal amount;
}
public class Test3 {
public static void main(String[] args){
List<User> userList1 = new ArrayList<>();
User user1 = new User("1001", "lili", new BigDecimal("100.5"));
User user2 = new User("1002", "lucy", new BigDecimal("100.66"));
userList1.add(user1);
userList1.add(user2);
List<User> userList2 = new ArrayList<>();
User user3 = new User("1001", "lili", new BigDecimal("200.5"));
User user4 = new User("1002", "lucy", new BigDecimal("200.66"));
User user5 = new User("1003", "jack", new BigDecimal("200.88"));
userList2.add(user3);
userList2.add(user4);
userList2.add(user5);
System.out.println("userList1:" + userList1);
System.out.println("userList2:" + userList2);
List<User> collect = userList1.stream().map(u1 -> {
userList2.stream().filter(u2 -> u2.getId().equals(u1.getId())).forEach(u2 -> {
if (u2.getAmount() != null) {
u1.setAmount(u1.getAmount().add(u2.getAmount()));
}
});
return u1;
}).collect(Collectors.toList());
System.out.println("userList1:" + userList1);
System.out.println("collect:" + collect);
System.out.println("userList2:" + userList2);
}
}
结果可见,相同 id 记录的 amount 属性值进行了累计运算。
结果可见,userList1 的值发生了改变,如果不想改变原集合内容:
public class Test3 {
public static void main(String[] args){
List<User> userList1 = new ArrayList<>();
User user1 = new User("1001", "lili", new BigDecimal("100.5"));
User user2 = new User("1002", "lucy", new BigDecimal("100.66"));
userList1.add(user1);
userList1.add(user2);
List<User> userList2 = new ArrayList<>();
User user3 = new User("1001", "lili", new BigDecimal("200.5"));
User user4 = new User("1002", "lucy", new BigDecimal("200.66"));
User user5 = new User("1003", "jack", new BigDecimal("200.88"));
userList2.add(user3);
userList2.add(user4);
userList2.add(user5);
ArrayList<User> result = new ArrayList<>();
List<User> collect = userList1.stream().map(u1 -> {
userList2.stream().filter(u2 -> u2.getId().equals(u1.getId())).forEach(u2 -> {
if (u2.getAmount() != null) {
BigDecimal add = u1.getAmount().add(u2.getAmount());
result.add(new User(u1.getId(), u1.getName(), add));
}
});
return u1;
}).collect(Collectors.toList());
System.out.println("userList1:" + userList1);
System.out.println("userList2:" + userList2);
System.out.println("collect:" + collect);
System.out.println("result: " + result);
}
}
新增集合 result 进行最终结果存储,原集合内容不变。
有时候查询多个结果集需要进行合并,比如:
List1:[ {id:1001, value1:100}, {id:1002, value1:200} ]
List2:[ {id:1001, value2:200} ]
List3:[ {id:1001, value3:300}, {id:1002, value3:300}, {id:1003, value3:300} ]
合并后:
List:[ {id:1001, value1:100, value2:200, value3:300}, {id:1002, value1:200, value3:300}, {id:1003, value3:300} ]
Total:
@Data
@AllArgsConstructor
public class Total {
public String id;
public String value1;
public String value2;
public String value3;
}
public class MergeValue {
public static void main(String[] args) {
ArrayList<Total> list1 = new ArrayList<>();
list1.add(new Total("1001","100","",""));
list1.add(new Total("1002","200","",""));
ArrayList<Total> list2 = new ArrayList<>();
list2.add(new Total("1001","","200",""));
ArrayList<Total> list3 = new ArrayList<>();
list3.add(new Total("1001","","","300"));
list3.add(new Total("1002","","","300"));
list3.add(new Total("1003","","","300"));
System.out.println(list1);
System.out.println(list2);
System.out.println(list3);
List<Total> collect = list1.stream().map(v1 -> {
list2.stream().filter(v2 -> v1.getId().equals(v2.getId())).forEach(v2 -> {
v1.setValue2(v2.getValue2());
});
list3.stream().filter(v2 -> v1.getId().equals(v2.getId())).forEach(v2 -> {
v1.setValue3(v2.getValue3());
});
return v1;
}).collect(Collectors.toList());
System.out.println(collect);
}
}
运行结果:
结果可见,“1003” 的记录没有统计,因为以 list1 为基准进行判断赋值,list1 没有 id=“1003” 的记。
解决方案:先获取所有id,再遍历。
public class MergeValue {
public static void main(String[] args) {
ArrayList<Total> list1 = new ArrayList<>();
list1.add(new Total("1001","100","",""));
list1.add(new Total("1002","200","",""));
ArrayList<Total> list2 = new ArrayList<>();
list2.add(new Total("1001","","200",""));
ArrayList<Total> list3 = new ArrayList<>();
list3.add(new Total("1001","","","300"));
list3.add(new Total("1002","","","300"));
list3.add(new Total("1003","","","300"));
System.out.println(list1);
System.out.println(list2);
System.out.println(list3);
// 1、获取所有集合中的 id
HashSet<String> strings = new HashSet<>();
list1.stream().forEach(v1 -> strings.add(v1.getId()));
list2.stream().forEach(v1 -> strings.add(v1.getId()));
list3.stream().forEach(v1 -> strings.add(v1.getId()));
// 2、将获取的 id 放到新的集合中,用于基准遍历
ArrayList<Total> result = new ArrayList<>();
strings.stream().forEach(v1 -> result.add(new Total(v1, "", "", "")));
System.out.println(result);
// 3、遍历比较赋值
List<Total> collect = result.stream().map(v1 -> {
list1.stream().filter(v2 -> v1.getId().equals(v2.getId())).forEach(v2 -> {
v1.setValue1(v2.getValue1());
});
list2.stream().filter(v2 -> v1.getId().equals(v2.getId())).forEach(v2 -> {
v1.setValue2(v2.getValue2());
});
list3.stream().filter(v2 -> v1.getId().equals(v2.getId())).forEach(v2 -> {
v1.setValue3(v2.getValue3());
});
return v1;
}).collect(Collectors.toList());
System.out.println(collect);
}
}
✨ 完美!!✨