aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Another from collections Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Another from collections" Watch "Another from collections" New topic
Author

Another from collections

Serch Hdez
Ranch Hand

Joined: May 01, 2008
Posts: 43
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

Joined: Mar 11, 2008
Posts: 11
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

Joined: May 01, 2008
Posts: 43
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

Joined: Mar 22, 2008
Posts: 265

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.)


There will always be people who are ahead of the curve, and people who are behind the curve. But knowledge moves the curve. --Bill James
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
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.


Thanks<br />Dinesh
Stevi Deter
Ranch Hand

Joined: Mar 22, 2008
Posts: 265

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Another from collections
 
Similar Threads
K & B - Generics & Collections
HasMap Problem using equals ANd hashCode methods
Please explain the last output of this example from kathy sierra book
Doubt in Collection
map get method doubt