jdk8使用stream实现两个list集合合并成一个(对象属性的合并)

目录

  • 一、前言
  • 二、示例
    • 示例1:java8 合并两个 list<map>
    • 示例2:java8 合并两个 list<T>
    • 示例3:java8 合并两个 list<T>,集合个数前者小于后者,要后者
    • 示例4:java8 合并两个 list<T>,集合个数前者大于后者,要后者
java使用stream实现list中对象属性的合并:根据两个List中的某个相同字段合并成一条List,包含两个List中的字段

一、前言 为什么要用Lambda表达式Stream流做集合的处理? 因为效率高、代码简洁、高端大气上档次!
【jdk8使用stream实现两个list集合合并成一个(对象属性的合并)】(1)描述:比如一个List集合中List<对象1>,该对象1包含A、B和C列; 另外一个List集合中List<对象2>,该对象2包含A、D列;
(2)问题:需要将两个List合成List<对象3>返回,该对象3包含A、B、C、D列
(3)实现:使用stream实现两个list集合合并成一个list集合
简要说明:filter的使用方式为: filter(item -> item的逻辑判断 ) ,其中filter会保留判断条件为true的记录

二、示例
示例1:java8 合并两个 list<map>
java8 合并两个 list<map>, 并将userId 相同的所有属性合并到一个map中
list1中对象的属性:userId、userName
list2中对象的属性:userId、gender、age
最总集合中对象的属性:userId、userName、gender、age
import java.util.*; import java.util.stream.Collectors; /** * @author qzz */public class ListTest {public static void main(String[] args) {mergeList(); }/*** @Description: 合并两个list,并将userId相同的其它属性合并* @Title: mergeList* @param: @return* @return: List* @throws*/public static List mergeList(){//构建List集合1List list1 = new ArrayList<>(); Map data=https://www.it610.com/article/new HashMap<>(); data.put("userId","100001"); data.put("userName","唐僧"); list1.add(data); data=https://www.it610.com/article/new HashMap<>(); data.put("userId","100002"); data.put("userName","八戒"); list1.add(data); data=https://www.it610.com/article/new HashMap<>(); data.put("userId","100003"); data.put("userName","悟空"); list1.add(data); data=https://www.it610.com/article/new HashMap<>(); data.put("userId","100004"); data.put("userName","沙僧"); list1.add(data); //构建List集合2List list2 = new ArrayList<>(); data=https://www.it610.com/article/new HashMap<>(); data.put("userId","100001"); data.put("gender","男"); data.put("age",20); list2.add(data); data=https://www.it610.com/article/new HashMap<>(); data.put("userId","100002"); data.put("gender","雄"); data.put("age",1000); list2.add(data); data=https://www.it610.com/article/new HashMap<>(); data.put("userId","100003"); data.put("gender","雄"); data.put("age",600); list2.add(data); data=https://www.it610.com/article/new HashMap<>(); data.put("userId","100004"); data.put("gender","男"); data.put("age",800); list2.add(data); //使用stream流把list1和list2根据属性userId合并一个list集合List list = list1.stream().map(m -> {list2.stream().filter(m2-> Objects.equals(m.get("userId"),m2.get("userId"))).forEach(m2-> {m.put("gender",m2.get("gender")); m.put("age",m2.get("age")); }); return m; }).collect(Collectors.toList()); for(Map map:list){System.out.println(map.toString()); }return list; }}

合并运行结果:
jdk8使用stream实现两个list集合合并成一个(对象属性的合并)
文章图片


示例2:java8 合并两个 list<T>
java8 合并两个 list<T>, 并将tickeId相同的所有属性合并到一个T中
list1中对象的属性:ticketId、ticketName
list2中对象的属性:ticketId、saleNum、batchAvailableNum
最总集合中对象的属性:ticketId、ticketName、saleNum、batchAvailableNum
import java.util.*; import java.util.stream.Collectors; /** * @author qzz */public class ListTest1 {public static void main(String[] args) {mergeList(); }/*** @Description: 合并两个list,并将ticketId相同的其它属性合并* @Title: mergeList* @param: @return* @return: List* @throws*/public static List mergeList(){//构建List集合1List list1 = new ArrayList<>(); Ticket data=https://www.it610.com/article/new Ticket(); data.setTicketId("100001"); data.setTicketName("唐僧"); list1.add(data); data=https://www.it610.com/article/new Ticket(); data.setTicketId("100002"); data.setTicketName("八戒"); list1.add(data); data=https://www.it610.com/article/new Ticket(); data.setTicketId("100003"); data.setTicketName("悟空"); list1.add(data); data=https://www.it610.com/article/new Ticket(); data.setTicketId("100004"); data.setTicketName("沙僧"); list1.add(data); //构建List集合2List list2 = new ArrayList<>(); data=https://www.it610.com/article/new Ticket(); data.setTicketId("100001"); data.setSaleNum("20"); data.setBatchAvailableNum("10"); list2.add(data); data=https://www.it610.com/article/new Ticket(); data.setTicketId("100001"); data.setSaleNum("20"); data.setBatchAvailableNum("10"); list2.add(data); data=https://www.it610.com/article/new Ticket(); data.setTicketId("100002"); data.setSaleNum("1000"); data.setBatchAvailableNum("600"); list2.add(data); data=https://www.it610.com/article/new Ticket(); data.setTicketId("100003"); data.setSaleNum("600"); data.setBatchAvailableNum("100"); list2.add(data); data=https://www.it610.com/article/new Ticket(); data.setTicketId("100004"); data.setSaleNum("800"); data.setBatchAvailableNum("300"); list2.add(data); //使用stream流把list1和list2根据属性ticketId合并一个list集合List list = list1.stream().map(m -> {list2.stream().filter(m2-> Objects.equals(m.getTicketId(),m2.getTicketId())).forEach(m2-> {m.setSaleNum(m2.getSaleNum()); m.setBatchAvailableNum(m2.getBatchAvailableNum()); }); return m; }).collect(Collectors.toList()); for(Ticket ticket:list){System.out.println(ticket.getTicketId()+","+ticket.getTicketName()+","+ticket.getSaleNum()+","+ticket.getBatchAvailableNum()); }return list; }}

合并运行结果:
jdk8使用stream实现两个list集合合并成一个(对象属性的合并)
文章图片


示例3:java8 合并两个 list<T>,集合个数前者小于后者,要后者
list1中对象的属性:ticketId、ticketName
list2中对象的属性:ticketId、batchId
最总集合中对象的属性:ticketId、ticketName、batchId
list1:
ticketId:10001 , ticketName:唐僧
ticketId:10002 , ticketName:八戒
list2:
ticketId:10001 , batchId:1
ticketId:10001 , batchId:2
ticketId:10002 , batchId:1
ticketId:10002 , batchId:2
ticketId:10003 , batchId:2
想要的结果:list1合并到 list 2
ticketId:10001 , ticketName:唐僧,batchId:1
ticketId:10001 , ticketName:唐僧,batchId:2
ticketId:10002 , ticketName:八戒,batchId:1
ticketId:10002 , ticketName:八戒,batchId:2
ticketId:10003 , ticketName:八戒,batchId:2
代码实现:
import java.util.ArrayList; import java.util.List; import java.util.Objects; import java.util.stream.Collectors; /** * @author qzz */public class ListTest2 {public static void main(String[] args) {mergeList(); }/*** @Description: 合并两个list,并将userId相同的其它属性合并* @Title: mergeList* @param: @return* @return: List* @throws*/public static List mergeList(){List list1 = new ArrayList<>(); Ticket data=https://www.it610.com/article/new Ticket(); data.setTicketId("100001"); data.setTicketName("唐僧"); list1.add(data); data=https://www.it610.com/article/new Ticket(); data.setTicketId("100002"); data.setTicketName("八戒"); list1.add(data); List list2 = new ArrayList<>(); data=https://www.it610.com/article/new Ticket(); data.setTicketId("100001"); data.setBatchId("1"); list2.add(data); data=https://www.it610.com/article/new Ticket(); data.setTicketId("100001"); data.setBatchId("2"); list2.add(data); data=https://www.it610.com/article/new Ticket(); data.setTicketId("100002"); data.setBatchId("1"); list2.add(data); data=https://www.it610.com/article/new Ticket(); data.setTicketId("100002"); data.setBatchId("2"); list2.add(data); data=https://www.it610.com/article/new Ticket(); data.setTicketId("100002"); data.setBatchId("3"); list2.add(data); //使用stream流把list1合并到list2集合中,根据ticketId属性List list = list2.stream().map(m -> {list1.stream().filter(m2-> Objects.equals(m.getTicketId(),m2.getTicketId())).forEach(m2-> {m.setTicketId(m2.getTicketId()); m.setTicketName(m2.getTicketName()); }); return m; }).collect(Collectors.toList()); for(Ticket ticket:list){System.out.println(ticket.getTicketId()+","+ticket.getTicketName()+","+ticket.getBatchId()); }return list; }}

合并运行结果:
jdk8使用stream实现两个list集合合并成一个(对象属性的合并)
文章图片


示例4:java8 合并两个 list<T>,集合个数前者大于后者,要后者
list1中对象的属性:ticketId、ticketName
list2中对象的属性:ticketId、batchId
最总集合中对象的属性:ticketId、ticketName、batchId
list1:
ticketId:10001 , ticketName:唐僧
ticketId:10002 , ticketName:八戒
list2:
ticketId:10001 , batchId:1
ticketId:10001 , batchId:2
想要的结果:list1合并到 list 2,过滤掉batch不存在的数据
ticketId:10001 , ticketName:唐僧,batchId:1
ticketId:10001 , ticketName:唐僧,batchId:2
代码实现:
import java.util.ArrayList; import java.util.List; import java.util.Objects; import java.util.stream.Collectors; /** * @author qzz */public class ListTest3 {public static void main(String[] args) {mergeList(); }/*** @Description: 合并两个list,并将userId相同的其它属性合并* @Title: mergeList* @param: @return* @return: List* @throws*/public static List mergeList(){List list1 = new ArrayList<>(); Ticket data=https://www.it610.com/article/new Ticket(); data.setTicketId("100001"); data.setTicketName("唐僧"); list1.add(data); data=https://www.it610.com/article/new Ticket(); data.setTicketId("100002"); data.setTicketName("八戒"); list1.add(data); List list2 = new ArrayList<>(); data=https://www.it610.com/article/new Ticket(); data.setTicketId("100001"); data.setBatchId("1"); list2.add(data); data=https://www.it610.com/article/new Ticket(); data.setTicketId("100001"); data.setBatchId("2"); list2.add(data); //data=https://www.it610.com/article/new Ticket(); //data.setTicketId("100002"); //data.setBatchId("1"); //list2.add(data); ////data=https://www.it610.com/article/new Ticket(); //data.setTicketId("100002"); //data.setBatchId("2"); //list2.add(data); ////data=https://www.it610.com/article/new Ticket(); //data.setTicketId("100002"); //data.setBatchId("3"); //list2.add(data); //使用stream流把list1合并到list2集合中,根据ticketId属性List list = list2.stream().map(m -> {list1.stream().filter(m2-> Objects.equals(m.getTicketId(),m2.getTicketId())).forEach(m2-> {m.setTicketId(m2.getTicketId()); m.setTicketName(m2.getTicketName()); }); return m; }).collect(Collectors.toList()); for(Ticket ticket:list){System.out.println(ticket.getTicketId()+","+ticket.getTicketName()+","+ticket.getBatchId()); }return list; }}

合并运行结果:
jdk8使用stream实现两个list集合合并成一个(对象属性的合并)
文章图片

到此这篇关于jdk8使用stream实现两个list集合合并成一个(对象属性的合并)的文章就介绍到这了,更多相关jdk8 stream list集合合并内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    推荐阅读