Optimize comparison and merging of the two lists

Viewed 423

I have two lists of Bill objects that have date field that represents month when the bill was created. I need to add some objects from bills list to oldBills. If there are no bill with the same data in oldBills list, then bill should be added.

I implemented it this way:

outer:
for (Bill bill : bills) {
  Date billDate = bill.getDate();
  for (Bill bill1 : oldBills) {
    Date bill1Date = bill1.getDate();
    if (Objects.equals(billDate, bill1Date)) {
      continue outer;
    }
  }
  newBills.add(bill);
}
oldBills.addAll(newBills);

but I think that this is not the best way.

May be you have any ideas, how to optimize this algorithm?

p.s.: java 7

5 Answers
Related