Java8 Stream对两个 List 遍历匹配数据的优化处理操作
使用场景,有两个List<Map<String,Object>>集合,第一个集合的所有元素都是需要保留的。
第一个集合的值为:
{name=张三丰1, id=1} {name=张三丰2, id=2} {name=张三丰3, id=3} {name=张三丰4, id=4} {name=张三丰5, id=5} {name=张三丰6, id=6} {name=张三丰7, id=7} {name=张三丰8, id=8}
第二个集合的值为:
{grade=61, id=1} {grade=62, id=2} {grade=63, id=3} {grade=64, id=4}
需要根据两个集合中id值相同,就把第二个集合中的grade值赋给第一个集合,如果不匹配,默认grade值为0
结果是这样:
{grade=61, name=张三丰1, id=1} {grade=62, name=张三丰2, id=2} {grade=63, name=张三丰3, id=3} {grade=64, name=张三丰4, id=4} {grade=0, name=张三丰5, id=5} {grade=0, name=张三丰6, id=6} {grade=0, name=张三丰7, id=7} {grade=0, name=张三丰8, id=8}
具体实现代码:
@Test public void demo01(){ List<Map<String,Object>> list = new ArrayList<Map<String,Object>>(); for (int i=1;i<9;i++){ Map<String,Object> map = new HashMap<>(); map.put("id",i); map.put("name","张三丰"+i); list.add(map); } Stream<Map<String, Object>> s1 = list.stream(); list.stream().forEach(map-> System.out.println(map)); List<Map<String,Object>> list2 = new ArrayList<Map<String,Object>>(); for (int i=1;i<5;i++){ Map<String,Object> map2 = new HashMap<>(); map2.put("id",i); map2.put("grade",i+60); list2.add(map2); } list2.stream().forEach(s-> System.out.println(s)); /** * List<Map<Object, Object>> resultList = oneList.stream().map(map -> twoList.stream() * .filter(m -> Objects.equals(m.get("id"), map.get("id"))) * .findFirst().map(m -> { * map.putAll(m); * map.put("grade",90); * return map; * }).orElse(null)) * .filter(Objects::nonNull).collect(Collectors.toList()); */ /* List<Map<String, Object>> resultList2 = list.stream().map(m->{ m.put("grade",0); for (int i=0;i<list2.size();i++){ if(m.get("id").equals(list2.get(i).get("id"))){ m.put("grade",list2.get(i).get("grade")); break; } } return m; }).collect(Collectors.toList());*/ List<Map<String, Object>> resultList2 = list.stream().map(m->{ m.put("grade",0); list2.stream().filter(m2->Objects.equals(m.get("id"), m2.get("id"))).forEach(s-> m.put("grade",s.get("grade"))); return m; }).collect(Collectors.toList()); resultList2.stream().forEach(s-> System.out.println(s)); }
补充知识:Java-8新特性-通过Stream获取两个List复杂对象的交并差集
思路:首先获取两个list的id,通过id比较获取id的交并差集,再通过一次获取list对象里面的交并差集元素
代码直接可运行,个人觉得java8的stream非常类似ES6的集合运算,filter、foreach、map、reduce基本可以一一对应
代码:
package com.stream; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; /** * * @ClassName: TwoListCopare * @Description: 两个List<对象>取交集\并集\差集</> **/ public class TwoListCopare { public static void main(String[] args) { UserDTO userOld1 = new UserDTO("1","aaa",22); UserDTO userOld2 = new UserDTO("2","bbb",32); UserDTO userOld3 = new UserDTO("3","ccc",11); UserDTO userOld4 = new UserDTO("4","ddd",42); UserDTO userOld5 = new UserDTO("5","bbb",22); UserDTO userOld6 = new UserDTO("6","eee",24); UserDTO userNew1 = new UserDTO("7","dada",22); //新增一个 UserDTO userNew2 = new UserDTO("2","bbb",32); //不变一个 UserDTO userNew3 = new UserDTO("3","kaka",33); //更新一个 UserDTO userNew4 = new UserDTO("8","dbdb",42); //新增一个 UserDTO userNew5 = new UserDTO("5","bbb",100); //更新一个 //当然,少了1,4,6 List<UserDTO> mapAdd = new ArrayList<>(); List<UserDTO> oldList = new ArrayList<>(); List<UserDTO> newList = new ArrayList<>(); //添加老数据 oldList.add(userOld1); oldList.add(userOld2); oldList.add(userOld3); oldList.add(userOld4); oldList.add(userOld5); oldList.add(userOld6); //添加新数据 newList.add(userNew1); newList.add(userNew2); newList.add(userNew3); newList.add(userNew4); newList.add(userNew5); //去交集,既获取id相同的交集,需要更新 //1.先提取出id和结果,用map形式 List<String> oldIds = new ArrayList<>(); List<String> newIds = new ArrayList<>(); oldList.stream().forEach(it->oldIds.add(it.getId())); newList.stream().forEach(it->newIds.add(it.getId())); // oldIds.stream().forEach(System.out::println); // newIds.stream().forEach(System.out::println); //取交集id System.out.println("-----------------交集----------------------"); List<String> collectUpdate = newIds.stream().filter(it -> oldIds.contains(it)).collect(Collectors.toList()); collectUpdate.stream().forEach(System.out::println); //取对应交集的对象 System.out.println("------------------交集的对象---------------------"); List<UserDTO> userUpdate = newList.stream().filter(it -> collectUpdate.contains(it.getId())).collect(Collectors.toList()); userUpdate.stream().forEach(System.out::println); //取old的差集 System.out.println("-----------------old的差集----------------------"); List<String> collectDelete = oldIds.stream().filter(it -> !newIds.contains(it)).collect(Collectors.toList()); collectDelete.stream().forEach((System.out::println)); //取对应old差集对象 System.out.println("-----------------old差集对象----------------------"); List<UserDTO> userDelete = oldList.stream().filter(it -> collectDelete.contains(it.getId())).collect(Collectors.toList()); userDelete.stream().forEach(System.out::println); //取new的差集 System.out.println("-----------------new的差集----------------------"); List<String> collectAdd = newIds.stream().filter(it -> !oldIds.contains(it)).collect(Collectors.toList()); collectAdd.stream().forEach((System.out::println)); //取对应old差集对象 System.out.println("-------------------old差集对象--------------------"); List<UserDTO> userAdd = newList.stream().filter(it -> collectAdd.contains(it.getId())).collect(Collectors.toList()); userAdd.stream().forEach(System.out::println); //取并集 System.out.println("-------------------并集--------------------"); List<String> allIds = new ArrayList<>(); //获取一个包含了oldIds和newIds的总结合,但是没有去重 allIds.addAll(oldIds); allIds.addAll(newIds); //去重,获取并集ids的新集合 List<String> joinIds = allIds.stream().distinct().collect(Collectors.toList()); joinIds.stream().forEach(System.out::println); } }
结果:
ps:并集对象集合并没有写,因为能够得到判断id自然就能得到了
以上这篇Java8 Stream对两个 List 遍历匹配数据的优化处理操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。
赞 (0)