• 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

hash code

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could somebody tell me what the hashcode exactly is?
Where to use it?
Is it anything related with the key value?
if I put:
HashMap hm = new HashMap();
hm.put( "one", new Integer( 1 ) );
System.out.println( HM.get( "one" ).hashCode() );
it prints out 1. but if I change Integer object to Float, it prints out 1065353216. what is that?
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you have a problem unrelated to hashing. When I change my Integer to Float, I do not have that problem.

The Object class has a hashCode() method that the HashMap uses, but you do not normally need to be concerned with it.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's funny I get the same number (1065353216) as well.
 
Ranch Hand
Posts: 1012
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmmm, me too... strange.
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this one:


For more info on the hashCode() method, check it out in the Object class.

It is used a lot internally by the java libraries, but developers don't need to concern themselves with the innards of this method very often. The HashMap class uses it to associate the specified value with the specified key, but it is all hidden (internal) stuff.

It is also used in referencing objects, so occasionally you will see a toString() method that does not override the toString() method of Object return a value like test@310d42 which is actually a hashCode of the actual reference address (more specifically, the unsigned hexadecimal representation of the hash code of the object).
[This message has been edited by Marilyn deQueiroz (edited July 24, 2001).]
 
Greg Harris
Ranch Hand
Posts: 1012
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oops, i copied christie's code and did not pay attention to the .hashCode() method... take that part out, christie, and your program will print "1".
 
christie xu
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot, every one. I know what is going on now.
 
You showed up just in time for the waffles! And this 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