• Post Reply Bookmark Topic Watch Topic
  • New Topic

List clear() method vs creating a new one every time wants to empty the list?  RSS feed

 
Ariana Hobson
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I used clear() method, it also wiped out the elements that I already added on tagsList. Googling why this happened, I read on SO it's because clear() will also wipe out the elements which was referenced by other object. In this case, since the objects in the tagsList are referring to "tests", so that's why it was emptied out as well. Am I correct in understanding this?

So I instantiate a new list with the same identifier. And it works like a charm. But what's the impact of this if I kept on instantiating a new list like this? Below code is a basic example. The real code would require me to add strings to a list, then add the list to another list and empty the previous list to enable addition of new strings. I will keep on iterating over this until it's finish. I wonder if this would slow down the performance..



Create a new list gives this result which is what I want:
Tests: [abc, def]
TagsList: [[abc, def]]

Tests after clear: []

Tests: [ghi]

TagsList: [[abc, def], [ghi]]


Using clear() method:
Tests: [abc, def]
TagsList: [[abc, def]]

Tests after clear: []

Tests: [ghi]

TagsList: [[ghi], [ghi]]
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ariana Hobson wrote:
So I instantiate a new list with the same identifier. And it works like a charm. But what's the impact of this if I kept on instantiating a new list like this? Below code is a basic example. The real code would require me to add strings to a list, then add the list to another list and empty the previous list to enable addition of new strings. I will keep on iterating over this until it's finish. I wonder if this would slow down the performance..


Performance isn't really a concern in this case, is it? You have two references that point to the same object -- the "tests" reference, and an element reference of the "tagList" list. Now, they both need to point to a list with different data. It is really not possible to have a single object satisfy both conditions. So, you need to have a different object ... it can't be avoided.

Henry
 
Stephan van Hulst
Saloon Keeper
Posts: 7961
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the ranch Ariana!

The difference between your two results shows exactly what's happening. In the second result, the two lists contained by tagsList are actually the same instance, so changing it will appear to change tagsList in two locations. As a matter of fact, at the end of your code, if you add another string to tests, and print tagsList again, you will see that the string will appear twice as well!

If you want the two lists to behave separately, they have to be different lists. You can only do this by creating new lists. This is not a matter of performance, this is a matter of writing code with the behavior you want.
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The impact is irrelevant in this case since using clear() gives you an incorrect (presumably) result.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

If you create new Lists, the old List objects will be available for garbage collection. That probably won't happen with a small app like yours, so all you are doing is filling a small amount of memory which the operating system will reclaim when your Java® app completes and the JVM exits.
 
Ariana Hobson
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK I've read your reply. Thanks! You can ignore my question below.
Campbell Ritchie wrote:Welcome to the Ranch

If you create new Lists, the old List objects will be available for garbage collection. That probably won't happen with a small app like yours, so all you are doing is filling a small amount of memory which the operating system will reclaim when your Java® app completes and the JVM exits.

___________________________________________________________________________________________________________

Thank you everyone for the prompt reply & for the warm welcome!

But why does instantiating a new list with the same identifier, wipe out the previous elements inside it?
I already instantiate a list called "tests"

Then I re-instantiate it again with the same identifier



I read in the Java documentation that instantiating an object means a new memory is allocated for the object. So does this mean that re-instantiating the same list will create a new object with the same identifier but allocated in a different memory location? What happened to the previous elements inside the list? Will the GC collect it?
Sorry I'm just learning java and would like to confirm my understanding because sometimes I might understand wrongly or not really sure if my understanding is the same as everyone.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't wipe anything out. What it does is take away the references to the original List. Until garbage collection occurs, the List object remains in memory on the heap. It still contains all its previous elements (or more precisely, references to them), and those elements may still be accessible from elsewhere in your program.
When you create a new List, it is created empty.
You are using the wrong words. You do not reinstantiate any Lists. You may instantiate the ArrayList class again and create a new List object, but you are not reinstantiating any Lists. You are creating a new one and moving it into the reference previously occupied by the older List. You cannot create the same object twice. But you can have multiple references to the same object. Or no references at all.
Yes, the old List loses its reference and becomes available for GC if GC actually occurs.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

And in this example, no GC occurs, as the previous List object is reachable from the other list.

Henry
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Until GC occurs you have several objects on the heap, but only one reference, so only one of them is referred to at any one time.
Those objects all occupy different memory locations. Have a look at the documentation for the equals method. Note the default implementation checks whether the two objects involved occupy the same memory location, i.e. they are both references to the same object. The code reads something like this:-
 
Ariana Hobson
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone!

I understand now. I learned a lot from just one post!
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!