Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Another from collections

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 265
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.)

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic