Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optimizing ArrayList.removeAll

A lot of people have said that ArrayList.removeAll is really slow with large size arrays.

This article provides two optimized solutions to the ArrayList.removeAll speed, but requires implementing them in the class itself, and cannot be used externally as a fix.

Is there any way to apply this sort of fix short of copying the ArrayList source code and using my own version of it?

Edit: I suppose I should add my need for this, as there is probably a way to do what I want without ArrayList.removeAll.

I have two lists of around 70,000 longs each. They are almost identical, but one list has a few more numbers that the second list doesn't have, and I want to find them. The only way I know to find them is to do first.removeAll(second) to find the difference. Is there another way?

like image 226
Rick Button Avatar asked Jul 24 '11 00:07

Rick Button


2 Answers

What about using a data structure that has a much better removal time, such as HashSet or TreeSet? So the big reason to use an arraylist is due to the fast access time O(1) to access records. But if you are trying to set difference then maybe you should use sets. Just a thought.

like image 74
Milhous Avatar answered Sep 28 '22 19:09

Milhous


You can create a subclass of ArrayList to optimize that method (and possibly others).

like image 37
Jeffrey Avatar answered Sep 28 '22 18:09

Jeffrey