Map is same for all the records, it will overwrite the existing records in the
Map with the same
In your case the
Key for all 3 records is
1 that is the reason, it is showing the latest record in the
Map in your final
You can go through the documentation of Map for further information.
Excerpt from the above mentioned documentation
- Map keys and values can be of any data type—primitive types, collections, sObjects, user-defined types, and built-in Apex types.
- Uniqueness of map keys of user-defined types is determined by the equals and hashCode methods, which you provide in your classes.
Uniqueness of keys of all other non-primitive types, such as sObject
keys, is determined by comparing the objects’ field values.
- Map keys of type String are case-sensitive. Two keys that differ only by the case are considered unique and have corresponding
distinct Map entries. Subsequently, the Map methods, including put,
get, containsKey, and remove treat these keys as distinct.
If the map previously contained a mapping for this key, the old value
is returned by the method and then replaced.
If the key is a string, the key value is case-sensitive.
Map<String, String> colorCodes = new Map<String, String>();
// Red is now #FF0000
Similarly, in your case, something like below is happenning
//1 is now Barbora Novotna
Maps, make sure you have key as unique otherwise it will override the existing values, in your case, you can switch the key value pair depending on your requirement i.e.
so that it gives you the count of records for each