If adding a pair key-value to Map it turns out that such a key already exists there, a new value is put into comformity with it.
In the Employee class equals () method is implemented in a way that two objects are considered equal if they contain the same reference to the string name (this.name == emp.name). Therefore, from two objects-keys containig the reference to the constant-string "Chandler" only one of them remains in Map. Count field is used to count the number of calls of Employee.equals (). When working with HashMap equals () method is called relatively seldom, for example, when the hash-code of a newly added key matches the hash-code of the previously added key - to reliably verify that it is the same object (equality of hash-codes does not guarantee equality of objects). In this example, it happens only once, during the second addition of Chandler.
Login in to like
Login in to comment