The Artima Developer Community
Sponsored Link

hashCode() should be overridden with equals()

Advertisement

Advertisement

This page contains an archived post to the Design Forum (formerly called the Flexible Java Forum) made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

hashCode() should be overridden with equals()

Posted by Jody Justus on 15 Sep 1998, 10:05 AM

Quoting the docs:

"If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result."

Otherwise, the object will not work properly as an element of a hashed collection (Set) or as a key in a hashtable.

See:
http://java.sun.com/products/jdk/1.1/docs/api/java.lang.Object.html#hashCode()
http://java.sun.com/products/jdk/1.1/docs/api/java.lang.Object.html#equals(java.lang.Object)

- -- J



Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us