List<Batch> vAllBatchList = getAllBatchCollection().toList(); //Has 700k records
List<Batch> vKeepableBatchCollection = getKeepableBatchCollection(pDaysKeepHistory).toList(); //has 600k records
vAllBatchList.removeAll(vKeepableBatchCollection);
In the above 3rd line removeAll method is taking too much time to finish. How to optimize the removeAll method here?
If you convert the List
of element to remove to a Set
, it should be faster:
vAllBatchList.removeAll(new HashSet<>(vKeepableBatchCollection));
This is assuming Batch
class overrides hashCode
and equals
properly.
Explanation: removeAll
for ArrayList
(I'm assuming your vAllBatchList
List
is an ArrayList
) iterates over all the elements of the List
on which it is called, and checks if the passed Collection
contains them. If the passed Collection
is a Set
, contains
will take expected constant time (O(1)
), while if the Collection
is a List
, it will take linear time (O(n)
).
Of course, if you can directly generate a Set
of the elements of vKeepableBatchCollection
instead of first creating a List
and then converting it to a Set
, it would be even better.