File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes hashCode( ) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "hashCode( )" Watch "hashCode( )" New topic
Author

hashCode( )

Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
I have the following output:

JOHN DOE : Java
Calculated hash=4 for JOHN DOE
students[hash]: JOHN DOE : Java
students hashCode value = 21990827

Could someone explain why I got the hashCode value = 21990827 and NOT 4? (4 is what I was expecting?



Do I need to have a toString( ) method? Similar to when I wish to have something more meaningful when printing out the contents of an array?

Thanking you in advance!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

You need to override hashCode() in Student. If you wanted the hashCode() for a Student object to be the same as the hashCode() for the Student's name, then you could write



[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: hashCode( )
 
Similar Threads
Arrays: Calling input from other methods and classes
hashing problem
How to choose correct hashCode method
Difference between hashCode() and equal()
cannot read a Collection of a custom class as a parameter