Java HashMap return value not confirming with my understanding of equals and hashcode

后端 未结 4 681
时光说笑
时光说笑 2021-01-23 08:01

The output of the following code sample is:

{1--e=e2, 2--e1=e1}

package com.sid.practice;

import java.util.HashMap;
import         


        
4条回答
  •  悲&欢浪女
    2021-01-23 08:27

    The reason behind the output {1--e=e2, 2--e1=e1} is :

    Map do not replaces key it only does so for value when there is match (present on the basis of key) in the existing Map. So applies in this case:

    Here e is equal to e2 for Map. When Map search for location to put m.put(e2, "e2"); it goes to location where e--"e1" is present and replaces "e1" by "e2" and leaves the key i.e e in this case intact

提交回复
热议问题