1

I have two lists namely,

List<Employee> oldEmplist and List<Employee> newEmplist and i have to compare both the list with only 2 properties of employee class namely empOrigId,empOrigNumberand these two properties will be set only for newEmplist so i have to compare these properties from newEmpList with empId,empNumber in oldEmpList. If this matches i have to replace the whole element in the oldEmpList with the matching item in the newEmplist.

Any suggestion how this can be done?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
ACP
  • 34,682
  • 100
  • 231
  • 371
  • http://stackoverflow.com/questions/1075656/simple-way-to-find-if-two-different-lists-contain-exactly-the-same-elements - although most approaches rely on the `equals` being defined on the objects as appropriate. If you need to do something "extra" in the operation - then get to writing code (e.g. a loop, perhaps with two iterators, and building an appropriate result). It is Java [7], after all. – user2864740 Jan 11 '14 at 10:22

1 Answers1

4
ListIterator<Employee> iterator = oldEmplist.listIterator();
while (iterator.hasNext()) {
    Employee x = iterator.next();
    for (Employee y : newEmplist) {
        if (x.empOrigId == y.empId && x.empOrigNumber == y.empNumber) {
            iterator.set(y);
            break;
        }
    }
}
Alon Gubkin
  • 56,458
  • 54
  • 195
  • 288