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

后端 未结 4 680
时光说笑
时光说笑 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:14

    Actually, you got it backwards. The value was overridden. The key wasn't replaced since as far as HashMap is concerned, e and e2 are identical.

    Your output is {1--e=e2, 2--e1=e1}:

    key = e, value = "e2" (which overrode the old value "e")
    key = e1, value = "e1" 
    

提交回复
热议问题