Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better practice to re-instantiate a List or invoke clear()

Tags:

java

list

Using Java (1.6) is it better to call the clear() method on a List or just re-instantiate the reference?

I have an ArrayList that is filled with an unknown number of Objects and periodically "flushed" - where the Objects are processed and the List is cleared. Once flushed the List is filled up again. The flush happens at a random time. The number within the List can potentially be small (10s of Objects) or large (millions of objects).

So is it better to have the "flush" call clear() or new ArrayList() ?

Is it even worth worrying about this sort of issues or should I let the VM worry about it? How could I go about looking at the memory footprint of Java to work this sort of thing out for myself?

Any help greatly appreciated.

like image 307
Phil Avatar asked Sep 29 '10 15:09

Phil


People also ask

How do you flush a list in Java?

The clear() method of List interface in Java is used to remove all of the elements from the List container. This method does not deleted the List container, instead it justs removes all of the elements from the List.

What does Clear () do in Java?

clear() method is used to remove all the elements from a Set. Using the clear() method only clears all the element from the set and not deletes the set. In other words, we can say that the clear() method is used to only empty an existing Set. Return Value: The method does not returns any value.

Can you instantiate a list?

Since list is an interface, one can't directly instantiate it. However, one can create objects of those classes which have implemented this interface and instantiate them. Few classes which have implemented the List interface are Stack, ArrayList, LinkedList, Vector etc.


2 Answers

The way you are using it looks very much like how a Queue is used. When you work of the items on the queue they are removed when you treat them.

Using one of the Queue classes might make the code more elegant.

There are also variants which handle concurrent updates in a predictable way.

like image 23
Peter Tillemans Avatar answered Sep 19 '22 22:09

Peter Tillemans


The main thing to be concerned about is what other code might have a reference to the list. If the existing list is visible elsewhere, do you want that code to see a cleared list, or keep the existing one?

If nothing else can see the list, I'd probably just clear it - but not for performance reasons; just because the way you've described the operation sounds more like clearing than "create a new list".

The ArrayList<T> docs don't specify what happens to the underlying data structures, but looking at the 1.7 implementation in Eclipse, it looks like you should probably call trimToSize() after clear() - otherwise you could still have a list backed by a large array of null references. (Maybe that isn't an issue for you, of course... maybe that's more efficient than having to copy the array as the size builds up again. You'll know more about this than we do.)

(Of course creating a new list doesn't require the old list to set all the array elements to null... but I doubt that that will be significant in most cases.)

like image 159
Jon Skeet Avatar answered Sep 18 '22 22:09

Jon Skeet