Java8 Stream对两个 List 遍历匹配数据的优化处理操作

网友投稿 268 2023-03-26

Java8 Stream对两个 List 遍历匹配数据的优化处理操作

使用场景,有两个List>集合,第一个集合的所有元素都是需要保留的。

第一个集合的值为:

{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> list = new ArrayList>();

for (int i=1;i<9;i++){

Map map = new HashMap<>();

map.put("id",i);

map.put("name","张三丰"+i);

list.add(map);

}

Stream> s1 = list.stream();

list.stream().forEach(map-> System.out.println(map));

List> list2 = new ArrayList>();

for (int i=1;i<5;i++){

Map map2 = new HashMap<>();

map2.put("id",i);

map2.put("grade",i+60);

list2.add(map2);

}

list2.stream().forEach(s-> System.out.println(s));

/**

* List> 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> resultList2 = list.stream().map(m->{

m.put("grade",0);

for (int i=0;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> 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,HxlLk通过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 mapAdd = new ArrayList<>();

List oldList = new ArrayList<>();

List 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 oldIds = new ArrayList<>();

List 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 collectUpdate = newIds.stream().filter(it -> oldIds.contains(it)).collect(Collectors.toList());

collectUpdate.stream().forEach(System.out::println);

//取对应交集的对象

System.out.println("------------------交集的对象---------------------");

List userUpdate = newList.stream().filter(it -> collectUpdate.contains(it.getId())).collect(Collectors.toList());

userUpdate.stream().forEach(System.out::println);

//取old的差集

System.out.println("-----------------old的差集----------------------");

List collectDelete = oldIds.stream().filter(it -> !newIds.contains(it)).collect(Collectors.toList());

collectDelete.stream().forEach((System.out::println));

//取对应old差集对象

System.out.println("-----------------old差集对象----------------------");

List userDelete = oldList.stream().filter(it -> collectDelete.contains(it.getId())).collect(Collectors.toList());

userDelete.stream().forEach(System.out::println);

//取new的差集

System.out.println("-----------------new的差集----------------------");

List collectAdd = newIds.stream().filter(it -> !oldIds.contains(it)).collect(Collectors.toList());

collectAdd.stream().forEach((System.out::println));

//取对应old差集对象

System.out.println("-------------------old差集对象--------------------");

List userAdd = newList.stream().filter(it -> collectAdd.contains(it.getId())).collect(Collectors.toList());

userAdd.stream().forEach(System.out::println);

//取并集

System.out.println("-------------------并集--------------------");

List allIds = new ArrayList<>();

//获取一个包含了oldIds和newIds的总结合,但是没有去重

allIds.addAll(oldIds);

allIds.addAll(newIds);

//去重,获取并集ids的新集合

List joinIds = allIds.stream().distinct().collect(Collectors.toList());

joinIds.stream().forEach(System.out::println);

}

}

结果:

ps:并集对象集合并没有写,因为能够得到判断id自然就能得到了

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> 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,HxlLk通过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 mapAdd = new ArrayList<>();

List oldList = new ArrayList<>();

List 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 oldIds = new ArrayList<>();

List 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 collectUpdate = newIds.stream().filter(it -> oldIds.contains(it)).collect(Collectors.toList());

collectUpdate.stream().forEach(System.out::println);

//取对应交集的对象

System.out.println("------------------交集的对象---------------------");

List userUpdate = newList.stream().filter(it -> collectUpdate.contains(it.getId())).collect(Collectors.toList());

userUpdate.stream().forEach(System.out::println);

//取old的差集

System.out.println("-----------------old的差集----------------------");

List collectDelete = oldIds.stream().filter(it -> !newIds.contains(it)).collect(Collectors.toList());

collectDelete.stream().forEach((System.out::println));

//取对应old差集对象

System.out.println("-----------------old差集对象----------------------");

List userDelete = oldList.stream().filter(it -> collectDelete.contains(it.getId())).collect(Collectors.toList());

userDelete.stream().forEach(System.out::println);

//取new的差集

System.out.println("-----------------new的差集----------------------");

List collectAdd = newIds.stream().filter(it -> !oldIds.contains(it)).collect(Collectors.toList());

collectAdd.stream().forEach((System.out::println));

//取对应old差集对象

System.out.println("-------------------old差集对象--------------------");

List userAdd = newList.stream().filter(it -> collectAdd.contains(it.getId())).collect(Collectors.toList());

userAdd.stream().forEach(System.out::println);

//取并集

System.out.println("-------------------并集--------------------");

List allIds = new ArrayList<>();

//获取一个包含了oldIds和newIds的总结合,但是没有去重

allIds.addAll(oldIds);

allIds.addAll(newIds);

//去重,获取并集ids的新集合

List joinIds = allIds.stream().distinct().collect(Collectors.toList());

joinIds.stream().forEach(System.out::println);

}

}

结果:

ps:并集对象集合并没有写,因为能够得到判断id自然就能得到了

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java Integer.valueOf()和Integer.parseInt()的区别说明
下一篇:历史天气数据接口 (历史天气数据库)(2分钟之前已更新)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~