Old values in hash map being overwritten by new values?

As for the size=9 but only 7 values in the table, you are misunderstanding the internal workings of the HashMap All values are not stored in the top-level table. The table is more like "buckets" that store entries grouped by certain hashcode ranges. Each "bucket" holds a chain of linked entries so what you are seeing in the table are just the first entries in each particular range chain.

The size is always correct though, in terms of total number of entries in the map.

As for the size=9 but only 7 values in the table, you are misunderstanding the internal workings of the HashMap. All values are not stored in the top-level table. The table is more like "buckets" that store entries grouped by certain hashcode ranges.

Each "bucket" holds a chain of linked entries so what you are seeing in the table are just the first entries in each particular range chain. The size is always correct though, in terms of total number of entries in the map. As for entries overwriting eachother, that happens only when you put en entry with a key that is identical (hashCode and equals) to en existing entry.So you are either adding with an existing key, or you are adding with null as key (null is permissible as key, but you can only have one entry with the key null).

Check your code, are you adding with null keys? If you are using instances of a custom class (one you created yourself) as key, have you implemented hashCode() and equals() according to the specifications (see download.oracle.com/javase/6/docs/api/ja...)? Are you making sure that you are really using unique keys for all 12 put operations?

If it were so, it would work correctly, so I assume, somewhere is a mistake. Please post your sourcecode. – dunni Jul 26 at 11:08 @Selva : If you are doing put() 12 times on your HashMap, and you only have 9 entries in the map after that, then you are not using unique keys.

Bear in mind the definition of unique in this context: hashCode() are not the same for the objects and equals() yields false. Either you have at least 4 keys that are equal, or you have incorrectly implemented hashCode() and equals() (if you are using a custom object as key). Post your code and we can help.

– pap Jul 26 at 11:20.

Make sure you use different keys. If that's the case, make sure equals and hashcode for your key class work as required, i.e. When two objects are equal, their hashcodes must be same.

And of course, equals for different key values (or what you'd expect to be distinct keys) must return false. If that doesn't help, post a minimal, yet complete (compilable) example that demonstrates your problem.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions