0

How to takeout attribute from two ArralyList of object which have different match in optimize way.

I have two array list of object and the after comaristion I am taking out the value which have difference based the the sttribute.

So In my condition when deptCOde is same in list but deptName is differnet then out put will be update deptName.

Here is my code.

public class educationMain {

    public static void main(String[] args) {
        
        List<person> list=new ArrayList<person>();  
        person l1 = new person(1,"Samual",100,"Sales","Business");
        person l2 = new person(2,"Alex",100,"Sales","Business");
        person l3 = new person(3,"Bob",101,"Engineering","Technology");
        person l4 = new person(4,"Michel",101,"Engineering","Technology");
        person l5 = new person(5,"Ryan",102,"PR","Services");
        person l6 = new person(6,"Horward",103,"Leadership","Managmnet");
        person l7 = new person(7,"Cyna",104,"HR","Human Resource");
        list.add(l1);  
        list.add(l2);  
        list.add(l3); 
        list.add(l4);  
        list.add(l5);  
        list.add(l6); 
        list.add(l7); 
        
        List<department> depList = new ArrayList<department>();
        
         department d1 = new department(100, "Sales","Business");
         department d2 = new department(101, "Engineering","Technology");
         department d3 = new department(102, "PR","Support");
         depList.add(d1);  
         depList.add(d2);  
         depList.add(d3); 

         List<person> listC = new ArrayList<person>();
         
         
         // My comparision Logic
         for(person p : list) {
             boolean  flag = false;
             for (department d:depList) {
                 if(p.deptCode == d.deptCode) {
                     if(p.deptName != d.deptName) {
                         p.deptName = d.deptName;
                         listC.add(p);
                     }
                 }
             }
         }
         
         for(person b:listC){  
             System.out.println(b.personId+" "+b.name+" "+b.deptCode+" "+b.parentDept+" "+b.deptName); 
         }
    }

}

This is code is working fine and Iam getting my output.

5 Ryan 102 PR Support

But instead of using two for loop do we have any efficient way to achive that.

David
  • 4,266
  • 8
  • 34
  • 69
  • Questions about code optimization is better asked at [codereview.se] – Jens Aug 13 '21 at 04:36
  • @Jens Thanks, I am going to ask there. Meanwhile here i have changed the question and looking forkey based approch. going to ask in code review. – David Aug 13 '21 at 04:38
  • You should read https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java/513839#513839 – tgdavies Aug 13 '21 at 04:41
  • @tgdavies I am not comparing string in java. I needto comapre list of object based onoperator without using loops. – David Aug 13 '21 at 04:45
  • `if(p.deptName != d.deptName) {` appears to compare Strings – tgdavies Aug 13 '21 at 04:52
  • @tgdavies yes that part is fine, but question is how can we avoid loops. anyway thanks for that link. helpfull. – David Aug 13 '21 at 05:16

2 Answers2

2

You can convert Department List into Map<DepartmentId, Department> in a single loop.

Map<Integer, Department> map = depList.stream()
  .collect(Collectors.toMap(department -> department.deptCode, Function.identity()));

Then you can use a single loop in comparison logic for e.g.

 // My comparision Logic
     for(person p : list) {
         Department d = map.get(p.deptCode);
         if(!d.deptName.equals(p.deptName)) {
           p.deptName = d.deptName;
           listC.add(p);
         }
     }
aditya lath
  • 410
  • 7
  • 6
  • Thanks for your answer. what is `Function.identity()` – David Aug 13 '21 at 04:55
  • It returns the same object on which you are using stream, Let me edit my comment, there should be depList instead of list in first code snippet. – aditya lath Aug 13 '21 at 04:59
  • Yes please, I am getting error in `Function.identity()` – David Aug 13 '21 at 05:00
  • Comparision logic is looks good, but in Map conversion I am getting error in `Function.identity`. Let me see what i cnado – David Aug 13 '21 at 05:03
  • Try to convert deptList into Map in anyway you are comfortable with, You can use simple For loop as well. – aditya lath Aug 13 '21 at 05:13
  • I ahve replaced with`department -> department` this and now after debugging I am getting the stuff but in last null pointer exception is coming. – David Aug 13 '21 at 06:28
0

Few optimisations :

  1. Sort Both the list based on department code.(use comparator & Collections.sort)

  2. Write a binary search instead of two for loops.

  3. Since both list are sorted keep searching in till (p.deptCode == d.deptCode) equals true otherwise break the loop.

marco525
  • 50
  • 11