• Post Reply Bookmark Topic Watch Topic
  • New Topic

Hash Map Behaviour  RSS feed

 
raghu kalachar
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I was just understanding Hash Map behavior in java.I tried to execute sample below program and as per my analysis it should print output as 'Mathew'.But it prints 'Jhon'.May I knwo reason for this.



 
Peter Muster
Ranch Hand
Posts: 74
5
Eclipse IDE Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This has nothing to do with the Map behaviour. Strings in Java are immutable. "Jhon" and "Mathew" are two different String instances. Your object e1 references a different object after you call setName a second time, but you store the previous String instance in your map. Also see here.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christian Pflugradt wrote:This has nothing to do with the Map behaviour. Strings in Java are immutable. "Jhon" and "Mathew" are two different String instances. Your object e1 references a different object after you call setName a second time, but you store the previous String instance in your map. Also see here.

Sorry, but this explanation is incorrect. Ok, I read the explanation again and I guess it's right. It's just worded in a way that was confusing me. My apologies.

e1 refers to the same object throughout the program, even after setName is called a second time.

On line 12, the object's name property is set to "Jhon"

On line 14, the object's name property, "Jhon", is added to the map as a value that is associated with a key of "1". Since "Jhon" is a String object, the map holds a reference to this, not the Employee object referenced by the variable e1.

On line 15, the object's name property is set to "Mathew". However, the value in the map that is associated with the key "1" still remains as the String "Jhon"

On line 16, the value in the map, which is still the String "Jhon", that is associated with the key "1" is displayed.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christian Pflugradt wrote:Your object e1 references a different object after you call setName a second time

This is what was throwing me off.

The variable e1 references the Employee object created on line 10. Variable e1 cannot reference a String because it is declared as an Employee.

It is the Employee object that references the String "Jhon" after its setName method is called the first time. When its setName is called a second time, it references the String "Mathew".
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a "graphical" representation:

 
Piet Souris
Master Rancher
Posts: 2042
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simply using: Map<Employee> map = new HashMap<>() would have prevented all problems.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:Simply using: Map<Employee> map = new HashMap<>() would have prevented all problems.

Erm, Map<String, Employee> surely?

Winston
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:Simply using: Map<Employee> map = new HashMap<>() would have prevented all problems.
Map<SomethingElse, Employee> surely?
 
Piet Souris
Master Rancher
Posts: 2042
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm... ehh, surely, yes.
 
raghu kalachar
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raghu kalachar wrote:Hi,

I was just understanding Hash Map behavior in java.I tried to execute sample below program and as per my analysis it should print output as 'Mathew'.But it prints 'Jhon'.May I knwo reason for this.







thank you guys.I have understood actual flow now.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!