• 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
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

HashMap and hashCode()

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[B][/B]

IT OUTPUTS
2

Why if hashCode is same 9;
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remember that the main requirement of the hashCode contract is that if two objects are equal according to the equals method, then they should have the same hashCode.

If you make the hashCode always return 9, and properly override equals, then you are fulfilling the contract.

It's not an error, it's just inefficienct.
[ April 04, 2007: Message edited by: Keith Lynn ]
 
author
Posts: 23923
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

IT OUTPUTS
2

Why if hashCode is same 9;



Just because two objects have the same hashcode, doesn't meant that they are equal. If the hashcode are the same, then the map uses the equals() method to further test for equality.

Henry
 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi sarwar,

You can refer to Map API to see what put method does.

Always refer API.
 
saqib sarwar
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1st thanks for prompt replies

NOW. as we know

[B][/B]

Outputs = false

SO



will return false for t1 and t2.

and obviously return false for t2 and t3.

than how

m.put(t1, "doLaundry"); // added to map OK
m.put(t2, "payBills"); // how can added to map if same hash as t1
m.put(t3, "cleanAttic"); // and how can t3 added to map if same hash as t1

kindly elaborate
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Notice you aren't creating new String objects, you are using String literals.
 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sarwar,

you are smart. i got you.

ToDos t1 = new ToDos("Monday"); // "Monday" created in pool
ToDos t2 = new ToDos("Monday"); // "Monday" refers to above in pool

Hence both the keys are equal by definition

public boolean equals(Object o) { return ((ToDos)o).day == this.day; }

If you don't want them to be overwritten try like this

ToDos t1 = new ToDos(new String("Monday"));
ToDos t2 = new ToDos(new String("Monday"));

Hope it clear.
Lets Rock
 
saqib sarwar
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks thoyyeti .

thanks a lot . i got it.
 
That's a very big dog. I think I want to go home now and hug this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic