File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hashCode() used for searching is right?

 
nagaraj raja
Ranch Hand
Posts: 36
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai i am thinking Hashtable,HashMap,HashSet are implemented hashing mechanisem ie it will generate hashCode this is useful searching the elements in a collection this is my openion os hashing is it right??
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the primary benefit of hashCode is fast lookup.

Imagine a library with numerous rows of bookshelves. If you're looking for a particular book, it would be a lot faster if you knew what particular row of shelves to look in, rather than searching through the entire library. Of course, when you've found the correct shelves (indicated by hashCode), there might be other books on those shelves (because different objects might return the same hashCode), but you've narrowed your search to a much smaller region.
 
Philip Heller
author
Ranch Hand
Posts: 119
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right, hash codes are used for efficient storage in collections. But as for sorting, only collections whose names begin with "Tree" sort their elements. See Chapter 8 of "Complete Java 2 Certification" for more.

-- Phil
 
Arulkumar Gopalan
Ranch Hand
Posts: 104
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
boolean equals(Object o) method also uses the hashing mechanism to compare object references.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic