两个list集合对象属性的合并
合并两个 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集合对象属性值运算
合并两个 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 进行最终结果存储,原集合内容不变。