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 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 + ")"; } }
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
If the generic classes of the two collections are the same, override the equals method and merge them directly using
set
. It's not that the same category cannot be merged.I don’t know if this is what you are talking about:
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.
@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:
Forgive me for being stupid, I didn’t understand the question. . . .
Assume objA#empNo is String
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