Java2个对象形集合按某一个属性合并
怪我咯
怪我咯 2017-04-18 10:35:35
0
7
483
怪我咯
怪我咯

走同样的路,发现不同的人生

reply all (7)
小葫芦

If the generic classes of the two collections are the same, override the equals method and merge them directly usingset. It's not that the same category cannot be merged.

    巴扎黑

    I don’t know if this is what you are talking about:

    package com.segmentfault.qa.java; import java.util.*; public class ListMergeTest { public static void main(String[] args) { List listA = new ArrayList<>(); List listB = new ArrayList<>(); Map> resultMap = new HashMap<>(); listA.add(new ClsA("001", "Alex")); listA.add(new ClsA("002", "Bill")); listA.add(new ClsA("003", "Carl")); listB.add(new ClsB("001", 1000)); listB.add(new ClsB("002", 2000)); listB.add(new ClsB("004", 4000)); for(ClsA a: listA){ Map map = new HashMap<>(); String empNo = a.getEmpNo(); for(ClsB b: listB){ if(b.getEmpNo().equals(empNo)){ map.put("money", b.getMoney()); } } map.put("name", a.getName()); resultMap.put(empNo, map); } System.out.println(resultMap); } } class ClsA{ private String empNo; private String name; public ClsA(String empNo, String name){ this.empNo = empNo; this.name = name; } public String getEmpNo() { return empNo; } public void setEmpNo(String empNo) { this.empNo = empNo; } public String getName() { return name; } public void setName(String name) { this.name = name; } } class ClsB{ private String empNo; private int money; public ClsB(String empNo, int money){ this.empNo = empNo; this.money = money; } public String getEmpNo() { return empNo; } public void setEmpNo(String empNo) { this.empNo = empNo; } public int getMoney() { return money; } public void setMoney(int money) { this.money = money; } }
      Ty80

      I feel that loops are definitely indispensable, but I feel that how to reduce loops is the optimization direction. The larger the amount of data in one loop is reduced, the more obvious the efficiency will be.

      package demo_java; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class Test1 { public static void main(String[] args) { List listA = new ArrayList<>(); List listB = new ArrayList<>(); // 构建10W数据 for(int i=0; i<100000; ++i) { ObjA a = new ObjA("no"+i, "name"+i); listA.add(a); } for(int i=8888; i<106666; ++i) { ObjB b = new ObjB("no"+i, "infos"+i); listB.add(b); } int aSize = listA.size(); int bSize = listB.size(); // listA Size: 100000 System.out.println("listA Size: " + aSize); // listB Size: 97778 System.out.println("listB Size: " + bSize); Long sTime = System.currentTimeMillis(); Map map = new HashMap<>(); // 减少一个循环,取最大的循环数 int maxSize = aSize > bSize ? aSize : bSize; for(int i=0; i
        阿神

        @witty Xiong Da , @martinwangjun’s answer idea should meet your needs. I modified another set of solutions based on his code, using a pure list method. The specific code is as follows:

        import java.util.ArrayList; import java.util.List; public class ListMergeTest_1 { public static void main(String[] args) { List listA = new ArrayList<>(); List listB = new ArrayList<>(); List listC = new ArrayList<>(); listA.add(new ClassA("001", "Alex")); listA.add(new ClassA("002", "Bill")); listA.add(new ClassA("003", "Carl")); listB.add(new ClassB("001", 1000)); listB.add(new ClassB("002", 2000)); listB.add(new ClassB("004", 4000)); for(ClassA a: listA){ String empNo = a.getEmpNo(); ClassC c = new ClassC(); c.setEmpNo(a.getEmpNo()); c.setName(a.getName()); for(ClassB b: listB){ if(b.getEmpNo().equals(empNo)){ c.setMoney(b.getMoney()); } } listC.add(c); } System.out.println(listC); } } class ClassA{ private String empNo; private String name; public ClassA(String empNo, String name){ this.empNo = empNo; this.name = name; } public String getEmpNo() { return empNo; } public void setEmpNo(String empNo) { this.empNo = empNo; } public String getName() { return name; } public void setName(String name) { this.name = name; } } class ClassB{ private String empNo; private int money; public ClassB(String empNo, int money){ this.empNo = empNo; this.money = money; } public String getEmpNo() { return empNo; } public void setEmpNo(String empNo) { this.empNo = empNo; } public int getMoney() { return money; } public void setMoney(int money) { this.money = money; } } class ClassC{ private String empNo; private String name; private int money; // public ClassC(String empNo, String name, int money){ // this.empNo = empNo; // this.name = name; // this.money = money; // } public String getEmpNo() { return empNo; } public void setEmpNo(String empNo) { this.empNo = empNo; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getMoney() { return money; } public void setMoney(int money) { this.money = money; } @Override public String toString() { return "(" + empNo + ", " + name + ", " + money + ")"; } }
          Peter_Zhu

          Forgive me for being stupid, I didn’t understand the question. . . .

            巴扎黑

            Assume objA#empNo is String

            Map map = new HashMap(); for (objA a: list_a) { map.put(a.empNo, a); } for (objB b: list_b) { map.put(b.empNo, b); } return map.values();
              PHPzhong

              Let me give you an idea. You change listB into a Map object, and use empNo as the key. And if you want to simulate a merge left join, you will definitely need a third object C to store the merged data of A and B. In this way, you use a.empNo when traversing listA. Go to mapB to get it. If you get it, merge it and add it to listC. If you don’t get it, continue traversing. Finally, if you need to use the list, you can use Map.values() to generate it

              This is a way to trade space for time, so that you don’t have to traverse listB every time you traverse listA, but the memory usage will be very large because there is an extra keySet

                Latest Downloads
                More>
                Web Effects
                Website Source Code
                Website Materials
                Front End Template
                About us Disclaimer Sitemap
                php.cn:Public welfare online PHP training,Help PHP learners grow quickly!