Search code examples
javacollectionsequalshashcode

Why doesn't ArrayDeque override equals() and hashCode()?


EDITED: Now only ArrayDeque is considered. (I originally thought LinkedList also doesn't override the two methods.)

Collection type ArrayDeque simply uses the hashCode and equals method implementations that it inherits from Object.

Why doesn't it instead override these methods with proper implementations (i.e. hash and equality test based on contained elements)?


Solution

  • LinkedList extends AbstractSequentialList which extends AbstractList which does override equals and hashCode - so the implementation is not inherited from Object.

    ArrayDeque, on the other hand, really doesn't inherit anything other implementation as far as I can see. Its direct superclass (AbstractCollection) doesn't override them. This feels like an exception rather than the rule - I believe most collection implementations in Java "do the right thing".

    I don't know of the justification for ArrayDeque choosing not to implement equality, but if you want to compare two deques you could easily just convert them into lists or arrays and do it that way.