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.