So far I have come up with a crude and inefficient method of removing duplicates:
This has many, many iterations through the array structures is there not a better method? Any suggestions are welcome!
Michael Labuschagne wrote:You have two ArrayLists containing objects of the same type in both... You want to process the two lists and remove duplicates (i.e. where an object is deemed to be the same as another object in the other ArrayList)... The objects in the individual ArrayLists do not contain duplicates and are sorted according to primary key (which is one of the test fields to determine two objects are the same - i.e. an object from list 1 with primary key 'PK1' could have a 'sister' element list 2 which also has primary key 'PK1'...
From your description, I suspect you could use a 'stepping' comparison of the two lists based on matching PKs. However, if it was me, I think I'd just load one of the lists (probably the larger one) into a HashSet and run contains().
However, in order for that to work, you'll have to make your equals() method do what isIdentical()(*) does currently, and also add a decent hashCode() method.
(*)Edit: BTW, that's not a great name for a method in my opinion. To me, "identical" means "has the same reference" (ie, obj1 == obj2).