时间:2023-01-19 09:30:18 | 栏目:JAVA代码 | 点击:次
java使用stream实现list中对象属性的合并:根据两个List中的某个相同字段合并成一条List,包含两个List中的字段
为什么要用Lambda表达式
和Stream流
做集合的处理? 因为效率高、代码简洁、高端大气上档次!
(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的记录
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<map>,并将userId相同的其它属性合并 * @Title: mergeList * @param: @return * @return: List<Map<String,Object>> * @throws */ public static List<Map<String,Object>> mergeList(){ //构建List集合1 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); data=new HashMap<>(); data.put("userId","100003"); data.put("userName","悟空"); list1.add(data); data=new HashMap<>(); data.put("userId","100004"); data.put("userName","沙僧"); list1.add(data); //构建List集合2 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); data=new HashMap<>(); data.put("userId","100003"); data.put("gender","雄"); data.put("age",600); list2.add(data); data=new HashMap<>(); data.put("userId","100004"); data.put("gender","男"); data.put("age",800); list2.add(data); //使用stream流把list1和list2根据属性userId合并一个list集合 List<Map<String, Object>> 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<String, Object> map:list){ System.out.println(map.toString()); } return list; } }
合并运行结果:
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<Ticket>,并将ticketId相同的其它属性合并 * @Title: mergeList * @param: @return * @return: List<Ticket> * @throws */ public static List<Ticket> mergeList(){ //构建List集合1 List<Ticket> list1 = new ArrayList<>(); Ticket data=new Ticket(); data.setTicketId("100001"); data.setTicketName("唐僧"); list1.add(data); data=new Ticket(); data.setTicketId("100002"); data.setTicketName("八戒"); list1.add(data); data=new Ticket(); data.setTicketId("100003"); data.setTicketName("悟空"); list1.add(data); data=new Ticket(); data.setTicketId("100004"); data.setTicketName("沙僧"); list1.add(data); //构建List集合2 List<Ticket> list2 = new ArrayList<>(); data=new Ticket(); data.setTicketId("100001"); data.setSaleNum("20"); data.setBatchAvailableNum("10"); list2.add(data); data=new Ticket(); data.setTicketId("100001"); data.setSaleNum("20"); data.setBatchAvailableNum("10"); list2.add(data); data=new Ticket(); data.setTicketId("100002"); data.setSaleNum("1000"); data.setBatchAvailableNum("600"); list2.add(data); data=new Ticket(); data.setTicketId("100003"); data.setSaleNum("600"); data.setBatchAvailableNum("100"); list2.add(data); data=new Ticket(); data.setTicketId("100004"); data.setSaleNum("800"); data.setBatchAvailableNum("300"); list2.add(data); //使用stream流把list1和list2根据属性ticketId合并一个list集合 List<Ticket> 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; } }
合并运行结果:
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<map>,并将userId相同的其它属性合并 * @Title: mergeList * @param: @return * @return: List<Map<String,Object>> * @throws */ public static List<Ticket> mergeList(){ List<Ticket> list1 = new ArrayList<>(); Ticket data=new Ticket(); data.setTicketId("100001"); data.setTicketName("唐僧"); list1.add(data); data=new Ticket(); data.setTicketId("100002"); data.setTicketName("八戒"); list1.add(data); List<Ticket> list2 = new ArrayList<>(); data=new Ticket(); data.setTicketId("100001"); data.setBatchId("1"); list2.add(data); data=new Ticket(); data.setTicketId("100001"); data.setBatchId("2"); list2.add(data); data=new Ticket(); data.setTicketId("100002"); data.setBatchId("1"); list2.add(data); data=new Ticket(); data.setTicketId("100002"); data.setBatchId("2"); list2.add(data); data=new Ticket(); data.setTicketId("100002"); data.setBatchId("3"); list2.add(data); //使用stream流把list1合并到list2集合中,根据ticketId属性 List<Ticket> 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; } }
合并运行结果:
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<map>,并将userId相同的其它属性合并 * @Title: mergeList * @param: @return * @return: List<Map<String,Object>> * @throws */ public static List<Ticket> mergeList(){ List<Ticket> list1 = new ArrayList<>(); Ticket data=new Ticket(); data.setTicketId("100001"); data.setTicketName("唐僧"); list1.add(data); data=new Ticket(); data.setTicketId("100002"); data.setTicketName("八戒"); list1.add(data); List<Ticket> list2 = new ArrayList<>(); data=new Ticket(); data.setTicketId("100001"); data.setBatchId("1"); list2.add(data); data=new Ticket(); data.setTicketId("100001"); data.setBatchId("2"); list2.add(data); // data=new Ticket(); // data.setTicketId("100002"); // data.setBatchId("1"); // list2.add(data); // // data=new Ticket(); // data.setTicketId("100002"); // data.setBatchId("2"); // list2.add(data); // // data=new Ticket(); // data.setTicketId("100002"); // data.setBatchId("3"); // list2.add(data); //使用stream流把list1合并到list2集合中,根据ticketId属性 List<Ticket> 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; } }
合并运行结果: