jose chiramal wrote:Also on what basis does a particular class override equals() or hashcode() methods.
A class overrides those methods if it needs to. It's as simple as that.
Arrays overload equals() method
Arrays is a utility method. You cannot create instances so there is no need to check for equality. It has overloaded equals to check if two arrays are equal. This is needed because equals for an array uses ==, not content equality.
ArrayList doesnt override equals() or hashcode() methods
LinkedList doesnt override equals() or hashcode() methods
Vector overrides equals and hashcode() methods
Stack doesnt override equals() or hashcode() methods
ArrayList and LinkedList (indirectly) both extend AbstractList which does override both. Apparently that implementation is good enough for ArrayList and LinkedList.
Vector is a synchronized class but AbstractList isn't. Therefore, Vector overrides these two methods only to make them synchronized like the rest of the class. These are their implementations at present:
Like I said, it only adds synchronization, nothing else.
HashTable overrides equls and hashcode() methods
Map overrides equals() method
HashMap doesnt override equals method
LinkedHashMap doesnt override equals method
Map overrides equals method only to specify that map equality means having the same mappings. It makes the contract for the method stricter, nothing more.
Hashtable (with t, not T) does not extend AbstractMap but Dictionary. Dictionary does not override equals. Therefore Hashtable needs to do so to stick to the contract of Map.equals.
HashMap and LinkedHashMap (indirectly) both extend AbstractHashMap which implements equals, in a way that is good enough for these two classes.
SortedMap doesnt override equals method
TreeMap doesnt override equals method
SortedMap inherits the contract on equals from Map. TreeMap inherits the implementation from AbstractMap.
Set overrides equals() method
HashSet doesnt override equals method
LinkedHashSet doesnt override equals method
SortedSet doesnt override equals method
TreeSet doesnt override equals method
See my story about Map.
Summarizing, if the implementation of the super class is good enough there is no need to override equals. With the List, Set and Map implementations the abstract implementations (AbstractList, AbstractSet, AbstractMap) have default implementations that are good enough for most concrete implementations.