Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Another from collections

 
Serch Hdez
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
given this code:


output is for str array: 1
something
output in list: 1
something

changes were reflected in both list and the array why is that?... also in the K&B book the map example that stars at 565



seems that if you change the name attribute on object d1 outside the map, it changes the object d1 originally stored in the map.. why is that?.. can anyone pls explain this a little bit more deeply? thanks in advance.
 
Meghna Rane
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Serch Hdez,
Answer to your first Question is,
When you use the asList() method, the array and
the List become joined at the hip. When you update one of them, the other gets
updated automatically.

& for your second question is that if you change the name attribute on object d1 outside the map,
it DOES NOT changes the object d1 originally stored in the map..
it can be explained this way...,

d1.name = "clover";
System.out.println(m.get(new Dog("clover"))); // #2

Here when you set
d1.name = "clover";
& call
m.get(new Dog("clover"))
it first checks for hash code using length of name (here it is 6) ,it matches with key
& then it is compared with equals() which also returns true (clover & clover matchs)

But taking example .,

d1.name = "arthur";
System.out.println(m.get(new Dog("clover")));

you can see hashcode matches(as both arthur & clover name length is 6)
but equals does't match(arthur != clover).

Hope you got your answer.
 
Serch Hdez
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your answer.. but I'm still not sure about it..

why changing the name attribute of d1.name="arthur" and looking at the map with map.get("clover") will be related on the test how will these two objects will test each other if one is outside the map? I guess that's what I'm not sure of it, also another question so only creating a List object with Arrays.asList will bound them together? if you create a Set myset = HashSet(Arrays.asList(str)); those will not be bounded?... thanks.


Regards.
 
Stevi Deter
Ranch Hand
Posts: 265
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Serch,

If you check the API for Arrays.asList(), you'll note it explains:


Returns a fixed-size list backed by the specified array. (Changes to the returned list "write through" to the array.)
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as know asList converts array to list .Correct me if iam wrong
Can you little elaborate on this problem. Please.
i am finding difficult to understand.
 
Stevi Deter
Ranch Hand
Posts: 265
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dinesh,

What are you still not clear about? Essentially your statement is true, Arrays.asList() takes an array and returns a List.

What's important to understand is that the list backed by the array.

That is why you can change an object referenced in the array or the List, and change will be seen in the other.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic