• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Journal Article - The SCJP Tip Line - hashCodes Uncovered

 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The just-released July 2004 edition of The JavaRanch Journal includes an article by Corey McGlone, "The SCJP Tip Line - hashCodes Uncovered".

Please use this thread to comment on and discuss the article.
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is this article still using the older Hashtable instead of HashMap? Time to upgrade.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Next Stepguy,

Welcome to Javaranch

We'd like you to read the Javaranch Naming Policy and change your publicly displayed name (change it here) to comply with our unique rule. Thank you.

I don't think it really matter what implementation of a hash one uses. The focus of the article was on the hashCode method. If you're interested in which hash implementation to use, refer to the link cited at the beginning of that article.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice article. Two comments:

1. There is very small error with Corey's third example (hold true to Rule #1 but not to Rule #2). The output does not correspond to the program. The actual output is:

1
2
3
4
6
6
7
8
9
10
Elapsed Time: 0 ms

Note the two 6s. This is because the keys array holds two objects that are created using the same values (MyObject(11,5)).

2. I was initially a bit confused by example 3 when I simply looked at the output. I expected there only to be one "10" string object in the hash table and the output appeared to show ten "10"s! I took another look at the code and I realized that the ten "10"s is what was expected from the actual code since the code get the elements from hash table using the 10 keys in the keys array.

It can be educational to inspect what keys are actually saved by the hash table.

For those readers wondering how to do this, you can do this with the following code:

When you run this, the output is:

The keySet in the hash table:
[[11,0], [11,1], [11,2], [11,3], [11,5], [11,6], [11,7], [11,8], [11,9]]
1
2
3
4
6
7
8
9
10

Then, when you fix hashCode to conform to the rules (e.g. return a), the output is:

The keySet in the hash table:
[[11,0]]
10


Cheers!
[ October 23, 2004: Message edited by: Brian S. Hayes ]
 
I'm full of tinier men! And a tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!