Java HashMap的返回值并不能证实我对equals和hashcode的理解 [英] Java HashMap return value not confirming with my understanding of equals and hashcode

查看:182
本文介绍了Java HashMap的返回值并不能证实我对equals和hashcode的理解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码示例的输出为:


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




  package com.sid.practice; 

import java.util.HashMap;
import java.util.Map;

public class InputOutputPractice
{

public InputOutputPractice()
{

}

public static void main(String [] args)
{
Employee e = new InputOutputPractice()。new Employee(1,e);
Employee e1 = new InputOutputPractice()。new Employee(2,e1);
Employee e2 = new InputOutputPractice()。new Employee(1,e2);

Map m = new HashMap();
m.put(e,e);
m.put(e1,e1);
m.put(e2,e2);
System.out.println(m);


$ b $ class class Employee
{
public Employee(int id,String name)
{
this.id = ID;
this.name = name;
}

private int id;
私人字符串名称;

public String getName()
{
return name;
}

public void setName(String name)
{
this.name = name;
}

public int getId()
{
return id;
}

public void setId(int id)
{
this.id = id;

$ b @Override
public boolean equals(Object obj)
{
return((Employee)obj).getId()==(this .getId());

$ b $ @Override
public int hashCode()
{
return Integer.valueOf(getId())。hashCode();
}

@Override
public String toString()
{
return this.id + - + this.name;
}
}
}

我不明白对象 e2 能够覆盖Object e 中的键,但不能覆盖该值。在我的理解中,输出应该是:

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



解决方案

其实,你已经把它倒过来了。该值被覆盖。关键是没有被取代,因为就 HashMap 而言,e和e2是相同的。



您的输出是 {1 - e = e2,2 - e1 = e1}

  key = e,value =e2(它覆盖旧值e)
key = e1,value =e1


The output of the following code sample is:

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

package com.sid.practice;

import java.util.HashMap;
import java.util.Map;

public class InputOutputPractice 
{

    public InputOutputPractice() 
    {

    }

    public static void main(String[] args) 
    {
        Employee e = new InputOutputPractice().new Employee(1, "e");
        Employee e1 = new InputOutputPractice().new Employee(2, "e1");
        Employee e2 = new InputOutputPractice().new Employee(1, "e2");

        Map m = new HashMap();
        m.put(e, "e");
        m.put(e1, "e1");
        m.put(e2, "e2");
        System.out.println(m);

    }

    class Employee
    {
        public Employee(int id, String name)
        {
            this.id=id;
            this.name = name;
        }

        private int id;
        private String name;

        public String getName() 
        {
            return name;
        }

        public void setName(String name) 
        {
            this.name = name;
        }

        public int getId() 
        {
            return id;
        }

        public void setId(int id) 
        {
            this.id = id;
        }

        @Override
        public boolean equals(Object obj) 
        {
            return ((Employee)obj).getId()==(this.getId());
        }

        @Override
        public int hashCode() 
        {
            return Integer.valueOf(getId()).hashCode();
        }

        @Override
        public String toString() 
        {
            return this.id + "--" + this.name;
        }
    }
}

I do not understand how the Object e2 was able to overwrite the key in Object e, but not the value. In my understanding the output should have been:

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

解决方案

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" 

这篇关于Java HashMap的返回值并不能证实我对equals和hashcode的理解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆