HashMap.put() is overriding existing item for different key

I have a HashMap which I am using to store objects of type SplitCriteria using a String as the key

Map<String, SplitCriteria> criteriaMap = new HashMap<String, SplitCriteria>();

A sample SplitCriteria object contains the something like the following:

SplitCriteria [
    id=4, 
    criteriaName="Location", 
    criteriaAbrevName="Loc", 
    fieldName="LOCATION", 
    isMandatory=false
]

with id being a long, isMandatory is a boolean and the rest are strings.

I am looping over previously populated Array of the same object type, total count is 7, adding each to the HashMap using the fieldName attribute as the key:

for(SplitCriteria split : selectedCriteria){
    String fieldName = split.getFieldName();
    criteriaMap.put(fieldName, split);
}

After this loop has finished, the size of the map appears to be 7, but looking at the table contents there are only 6 objects present.

From researching the issue, I have come to understand that if there is a clash with keys, the clashing objects are "chained" together using the next attribute of the entry in the Map.

From the image below, you can see this is what is happening in my scenario, but the two keys are completely different!

enter image description here

Also I read this in the docs for the put method

If the map previously contained a mapping for the key, the old value is replaced by the specified value

and

Returns: the previous value associated with key, or null if there was no mapping for key.

So if the keys were clashing, I would expect the old entry to be returned, but it is not.

I have no clue how this is happening, as each key I am using is completely different to the next.

Any help in resolving this would be greatly appreciated.

Paddy

EDIT: When I try and retrieve the object at a later stage I am getting a null reponse

SplitCriteria criteria = (SplitCriteria) criteriaMap.get(key);
Jon Skeet
people
quotationmark

but looking at the table contents there are only 6 objects present

Nope, look at size - it's 7. You've just got two values in the same bucket. They don't collide by exact hash value, but they do collide by bucket. That's fine.

You won't be able to observe that when you use the map - if you just use the public API, you'll see all 7 entries with no hint of anything untoward. This is why I would generally recommend avoiding digging into the internal details of an object using the debugger until you're really really sure there's a problem.

people

See more on this question at Stackoverflow