This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I have doubt that why size is not 3 as we have inserted 3 items. I have implemented hashCode and equals both. We know that two same items can go into one bucket. Does it depends on implementation of HashMap that it can have only one item (key-value) in bucket...do not contain duplicates when hashCode() is implemented and provides same int value for equals object.
Could anybody explain actual fundamental behind this.
We know that two same items can go into one bucket
No, HashMap does not accept duplicates. In fact two objects with the same hashCodes go into one bucket. There can be many different key objects in the same bucket. Two objects with same hashCodes are not considered same.
Car car1= new Car(2005); Car car2= new Car(2005);
You created two objects that are meaningfully the same. Try equals on them. And then used them as keys in a HashMap that, as I said does not accept duplicates.
Above invocation returns the old value assigned previosly to the car1 key. Check it. You have just replaced the value of ACCENT1 by ACCENT2 assigned with car1, and have not add new entry. [ July 31, 2006: Message edited by: Jasiek Motyka ]