Hi Anh,
The good example is the String class. The Object class computes the
hashCode based on the integer that represents its handle (I think). So
that two different object instances are considered as different. So, if
you add two Object objects to a HashSet, they will both exist.
String redefines the hashCode based on its content. So:
String string1 = new String("abc");
String string2 = new String("abc");
they have the same hashCode.
Let's suppose that the String didn't properly define the hashCode. Then:
HashSet h = new HashSet();
h.add(string1); h.add(string2);
would make h to contain TWO strings, although they are identical in content.
This would be puzzling, because the set would contain two identical
objects, as their "equals()" method show they are identical. (By
definition, a set should contain only one instance of each object).
The idea is, thus, to have a "hashCode" method compatible with the
"equals" method (equal objects should produce the same hash code).
For e.g., for a Person with String firstName and String lastName, you
can define a equals() override based on the two names and override the
hashCode to return a combination of the hashCodes of the two strings.
For e.g.:
@Override
public int hashCode(){
int n1 = firstName == null ? 0 : ( firstName.hashCode() % 46339);
int n2 = lastName == null ? 0 : ( lastName.hashCode() % 46339);
return n1 * 46339 + n2;
}
(this because 46339 is about the square root of MAXINT).
or something like this.
Hope it helps
mihai
[email protected] a écrit :
What is the best way (algorithm) to implement the hashCode method?
What is the consequence of not implementing the hashCode method and
say addiding it to a Set?
On Apr 15, 3:14 pm, Mihai DINCA <[email protected]> wrote:
Hi Cecil
Java says that if a.equals(b) is true, than a.hashCode() must be
identical to b.hashCode(). I.e. two objects are equal if and only if
their hasCode are equal.
The hashCode() is internally used by various library classes, for e.g.
all the hashtable based collections.
Let's say you have a HashSet and two different String objects:
HashSet h = new HashSet();
String str1 = args[0];
String str2 = args[1];
Then you add the two strings to the HashSet:
h.add(s1); h.add(s2);
If the content of s1 is identical to s2 (for e.g. they both contain
"Hello World"), then the HasSet will contain only one element. This
happens because hashCode() is overriden in the String class in order to
be compatible with the "equals" method and the HashSet is based on the
hashCode of the added objects.
You must use @Override since Java 5 (is is not mandatory, you get a
warning only if you don't do it). This is a supplementary protection
against mistakes. If you define a method "equals" but you didn't want to
override the original one (you just forgot there is a standard "equals"
method), then you get a warning. If you want to override the original
"equals" but you misspell the name, such as "@Override ... eguals(Object
o) ..." or you have errors in the definition, such as "@Override ...
equals(Person p) ..." (it is an error, because it is not an override:
the original method is ... equals(Object o) ...", then you get an error
message. So you have more notifications at compile time and less
surprises at execution time.
Hope it helps
mihai
PS: I saw the example "@Override ... equals(Person p) ..." somewhere, I
think it was just in the @javapassion course
Cecil H a écrit :
Exercise 5.1 class Card3.java and method hashCode(), whats the purpose
of this method its not called from anywhere? Is it also necessary to
add the @Override to the methods: public boolean equals(Object obj),
public int hashCode(), public String toString()
The program runs fine. I'm thinking the hashCode method may have been
put in there to randomize the cards and suit, if user input was used.
But since we explicitly declare the card and suit in the main method,
its kind of pointless.
--
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group
athttp://groups.google.com/group/javaprogrammingwithpassion?hl=en
--
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en