Isn't the JVM smart enough to efficiently put an object in a bucket
I would say override equals() whenever you have two objects that can be meaningfully equal without being the same instance.
Override hashCode() any time you overide equals()
Note that the JVM doesnt handle this (put an object in a bucket).
bucketIndex = object.hashCode() % array.length;
Collections classes are just normal java classes. There is no *special* JVM support for Maps or Lists or Sets, only arrays. Internally a java.util.HashMap is implemented using a plain old array. The indices of that array are finite (although as the load of the Map increases, a new larger array is allocated and all the key , value pairs are rehashed and their indices recalculated). I was just trying to point out that none of this is decided by the JVM, only by the algorthms used to implement the data structure.
I'm talking about Collections, not an array.
Ok, thanks for the reply but that didn't clear things up for me...
Let me ask another way. Which methods in which collections will automagically call hashCode() and equals() for me, behind the scene?
Originally posted by Chris Ringer:
The List classes don't care. They just operate on index.