Reputation: 79
I have two list of Objects. I have framed this list by querying the database.
For Ex:
List<Employee> firstList={holDate,holName,createdby,empId}
List<Employee> seconList={holDate,holName,createdby,empId}
Now, I need to compare holDate
, holName
of firstList
with holDate
, holName
of secondList
. If holDate
, holName
of firstList
not found in secondList
, I need to add that in separate list.
The list may be in any order.
NOTE: empId is a primary key column.
Update - what I've tried:
for(Employee emp:firstList) {
for(Employee tgtEmp:seconList) {
if((emp.getHolDate()!=tgtEmp.getHolDate())&& (emp.getHolName()!=tgtEmp.getHolName())){
printList.add(emp); break;
}
}
}
Sample List Values:
firstList={{2015-08-15,"Independence Day","e1","Empl"},{2015-01-26,"Republic Day","e1","Empl"},{2015-09-20,"Memorial Day","e1","Empl"}}
seconList={{2015-08-15,"Independence Day","e1","Emp2"},{2015-10-25,"Thanks Giving Day","e1","Emp2"}}
Here, newlist should have all the values which is available in seconList and "RepublicDay","MemorialDay" from firstList
Upvotes: 1
Views: 8255
Reputation: 2602
I think the above answer is not generically. Think if you add any new field on the Emp object then you need to update the equals method. If you are open to accepting new solution then please consider the below answer
Upvotes: 0
Reputation: 11298
Check this
for (Employee emp : firstList) {
boolean found=false;
for (Employee tgtEmp : seconList) {
if ((emp.getHolDate().equals(tgtEmp.getHolDate())) && (emp.getHolName().equals(tgtEmp.getHolName()))) {
found=true;
break;
}
}
if(!found){
printList.add(emp);
}
}
Upvotes: 1