Equal & Hashcode methods of object
The hashCode() & equals() methods gets inherited from the Object class, thus an instance of any object can make a call to hashCode().
The signature of hashCode is:
public boolean equals(Object obj)
public int hashCode();
The int value returned from hashCode() is of particular use with the hash based Collection classes:
- HashTable
- HashMap
- HashSet
The nature of hash based collections is to store keys and values. The key is what you use to look up a value. Thus you could for instance use a HashMap to store employee id in the key value and the employee name in the value part. This type of collection provides for extremely fast lookups.
The default implementation of Sun's SDK hashCode() method, is a return value that will be the memory address of the object.
Java allows any object to be used as a key in a hash table.
When storing objects in a hash, Java uses the hashCode() method which is a method that returns a hash code value for the object. The hashCode() method is supposed to return an int that should uniquely identify different objects. This method is supported for the benefit of hashtables such as those provided by java.util.Hashtable. A properly written hashCode() method will follow these the following rules:
1.) It is repeatable: hashCode(x) must return the same int when called
again unless set methods have been called.
2.) It is symmetric : if x.equals(y), then x.hashCode()
must == y.hashCode(), i.e., either both return
true, or both return false.
3.) If !x.equals(y) : it is not required that
x.hashCode() != y.hashCode, but doing so may
improve performance of hash tables, i.e., hashes
may call hashCode() before equals().
---------------------------------------------------------------------------------------------
The hashCode() & equals() methods gets inherited from the Object class, thus an instance of any object can make a call to hashCode().
The signature of hashCode is:
public boolean equals(Object obj)
public int hashCode();
The int value returned from hashCode() is of particular use with the hash based Collection classes:
- HashTable
- HashMap
- HashSet
The nature of hash based collections is to store keys and values. The key is what you use to look up a value. Thus you could for instance use a HashMap to store employee id in the key value and the employee name in the value part. This type of collection provides for extremely fast lookups.
The default implementation of Sun's SDK hashCode() method, is a return value that will be the memory address of the object.
Java allows any object to be used as a key in a hash table.
When storing objects in a hash, Java uses the hashCode() method which is a method that returns a hash code value for the object. The hashCode() method is supposed to return an int that should uniquely identify different objects. This method is supported for the benefit of hashtables such as those provided by java.util.Hashtable. A properly written hashCode() method will follow these the following rules:
1.) It is repeatable: hashCode(x) must return the same int when called
again unless set methods have been called.
2.) It is symmetric : if x.equals(y), then x.hashCode()
must == y.hashCode(), i.e., either both return
true, or both return false.
3.) If !x.equals(y) : it is not required that
x.hashCode() != y.hashCode, but doing so may
improve performance of hash tables, i.e., hashes
may call hashCode() before equals().
---------------------------------------------------------------------------------------------