3

Possible Duplicate:
list.clear() vs list = new ArrayList<int>();

I have a list:

List<Integer> l1  = new ArrayList<Integer>();

and added some integer variables on that list. After some operation, I need to reuse that list, so I set it to null.

Which is better, setting to null or using .clear()? Or is there another way?

Community
  • 1
  • 1
MGPJ
  • 1,062
  • 1
  • 8
  • 15

5 Answers5

2
  1. l1 = null will destroy the reference to the list object and it will be garbage collected (assuming there are no other references to the List).

  2. l1.clear() will iterate the entire list and clear all of its values (i.e. setting the objects at each index to null).

The first doesn't require iterating through the entire list so it is usually faster. If for whatever reason you need to reuse the List object, you should use l1.clear() to remove the objects from the list.

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
  • Are you sure ArrayList#clear() will iterate the entire list and set each value to `null`? Reading [the documentation](http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html#clear%28%29) does not really state anything like that and it would actually be a pretty naive implementation. Also, when it would set each object to `null` it would _not_ be empty afterwards! – Simon Lehmann Jul 02 '12 at 15:06
  • @SimonLehmann, yes I am sure. Check out the [**source code**](http://docjar.com/html/api/java/util/ArrayList.java.html). – Alex Lockwood Jul 02 '12 at 15:25
  • Well, that is certainly unexpected (for me at least). Having thought about it a bit more, it is also a valid implementation decision, as it prevents reallocation of the array, which might be more expensive (maybe they tried that). Would be nice to have this information in the user documentation! – Simon Lehmann Aug 03 '12 at 13:43
2

l1 = null; assigns null to the l1 variable. If the list has been stored before in another object, this other object still has a reference to the list, and thus still has access to all the elements that are stored in this list. If the List object object previously referenced by l1 is not reachable anymore, it will be garbage collected. Same for its elements: if they're not reachable anymore, they will be garbage collected.

l1.clear(); is very different: it removes all the elements from the list. If the list has been stored before in another object, this other object has a reference to the list you just cleared, and thus doesn't have access to the elements that were stored in the list anymore (since you removed them). If the elements previously stored in the list are not reachable anymore, they will be garbage collected. The list won't be, since you keep a reference to it (l1).

So , l1 = null should be used if you want to reuse the variable, but want to keep the List Object as is. l1.clear() should be used if you want to remove all the elements from the list.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • "If the list has been stored before in another object, this other object has a reference to the list you just cleared" this right here is the important part I think. `l1 = null;` and `l1.clear()` are NOT equivalent, and the OP should use the one that does what he wants. – millimoose Jul 02 '12 at 15:02
0

are you trying to clear the contents of the list, or start all over with a new dimension, new array? Just Garbage collect the array and call l1 = new Array(); and you'll generate a new array with null values, instead of going through and trying to clear the whole list.

Matt Westlake
  • 3,499
  • 7
  • 39
  • 80
0

If you are using a LinkedList, clear() will be freakin' fast and iteration too. But single access will be slower. Your choice depends on the use case...

Florent
  • 12,310
  • 10
  • 49
  • 58
  • 2
    The OP's not really using a `LinkedList` though. And optimizing for `clear()` over dropping the list on the ground isn't really useful very often. – millimoose Jul 02 '12 at 15:00
0

If you set it to null, then you have to initialize it again before using it. So I would just do:

l1  = new ArrayList<Integer>();

Which I think will be faster than calling .clear()

Forte L.
  • 2,772
  • 16
  • 25