java - 具有覆盖 equals 和 hashCode 的 HashMap 不起作用

标签 java hashmap

抱歉...关于这个愚蠢的问题,伙计们:

为什么没有应用 equals()hashCode()

目前,它们仅按我对 HashSet 的预期工作。

更新

EVEN键值5重复但不调用equals和hashCode。

我想将它也应用到 Value 上。

正如本例中 HashSet 调用 equal 和 hashCode 一样,为什么即使对于 key,hashMap 也没有被调用 equals 和 hashCode。

更新2 - 答案

将调用 HashMap 的 key(class->HashCode, equals)。 谢谢你们。 我对此有点困惑。 :)

    public class Employee {

        int id;
        String name; 
        int phone;

        public Employee(int id, String name, int phone) {
            this.id = id;
            this.name = name;
            this.phone = phone;
        }    
    // Getter Setter

        @Override
        public boolean equals(Object obj) {

            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            final Employee other = (Employee) obj;
            System.out.println("Employee -  equals" + other.getPhone());
            if (this.id != other.id) {
                return false;
            }
            if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
                return false;
            }
            if (this.phone != other.phone) {
                return false;
            }
            return true;
        }

        @Override
        public int hashCode() {
            System.out.println("Employee -  hashCode" );
            int hash = 3;
            hash = 67 * hash + this.id;
            hash = 67 * hash + (this.name != null ? this.name.hashCode() : 0);
            hash = 67 * hash + this.phone;
            return hash;
        }
    }

____________________________________________________________________________________

public class MapClass {

    public static void main(String[] args) {
        Map<Integer,Employee> map = new HashMap<Integer,Employee>();
        map.put(1, new Employee(1, "emp", 981));
        map.put(2, new Employee(2, "emp2", 982));
        map.put(3, new Employee(3, "emp3", 983));
        map.put(4, new Employee(4, "emp4", 984));
        map.put(5, new Employee(4, "emp4", 984));
       **//UPDATE**
        map.put(5, new Employee(4, "emp4", 984));            

        System.out.println("Finish Map" + map.size());
        Set<Employee> set = new HashSet<Employee>();

        set.add(new Employee(1, "emp", 981));
        set.add(new Employee(2, "emp2", 982));
        set.add(new Employee(2, "emp2", 982));
        set.add(new Employee(3, "emp3", 983));
        set.add(new Employee(4, "emp4", 984));
        set.add(new Employee(4, "emp4", 984));

        System.out.println(set.size());
    }
}

输出是

Finish Map5
Employee -  hashCode
Employee -  hashCode
Employee -  hashCode
Employee -  equals982
Employee -  equals982
Employee -  hashCode
Employee -  hashCode
Employee -  hashCode
Employee -  equals984
Employee -  equals984
4

最佳答案

EVEN key value 5 is repeated but it doesn't call equals and hashCode

是的,它确实在键上调用了 hashCode,即 Integer。

I want to apply it also on Value

现实情况:Java HashMap 并不是这样工作的。他们只检查键是否有重复项,而不检查值,这是应该的。

如果您希望在 Map 中检查 Employee 的哈希值,那么它必须是键。期间。

另一种可能的解决方案是下载可用的多重 map 。

编辑以查看它正在调用 hashCode 和 equals,将 map 的键类型更改为如下所示:

class MyInt {
   private Integer i;

   public MyInt(Integer i) {
      this.i = i;
   }

   public Integer getI() {
      return i;
   }

   @Override
   public int hashCode() {
      System.out.println("MyInt HashCode: " + i.hashCode());
     return i.hashCode();
   }

   @Override
   public boolean equals(Object obj) {
      System.out.printf("MyInt equals: [%s, %s]%n", i, obj);
      if (this == obj)
         return true;
      if (obj == null)
         return false;
      if (getClass() != obj.getClass())
         return false;
      MyInt other = (MyInt) obj;
      if (i == null) {
         if (other.i != null)
            return false;
      } else if (!i.equals(other.i))
         return false;
      return true;
   }

   @Override
   public String toString() {
      return i.toString();
   }

}

然后像这样填充你的 map :

   Map<MyInt,Employee> map = new HashMap<MyInt,Employee>();
   map.put(new MyInt(1), new Employee(1, "emp", 981));
   map.put(new MyInt(2), new Employee(2, "emp2", 982));
   map.put(new MyInt(3), new Employee(3, "emp3", 983));
   map.put(new MyInt(4), new Employee(4, "emp4", 984));
   map.put(new MyInt(5), new Employee(4, "emp4", 984));
   map.put(new MyInt(5), new Employee(4, "emp4", 984));

你会看到:

MyInt HashCode: 1
MyInt HashCode: 2
MyInt HashCode: 3
MyInt HashCode: 4
MyInt HashCode: 5
MyInt HashCode: 5
MyInt equals: [5, 5]

关于java - 具有覆盖 equals 和 hashCode 的 HashMap 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10702259/

相关文章:

java - 在 Java 8 中迭代 HashMap 时出现稳定的元素排序问题

java - 多图空间问题 : Guava

java - Spring 和 HTTP 选项请求

Java SWT - 将数据从组件返回到其他线程的最佳方式

java - 从 Activity 中更新 fragment ListView

java - HashMap作为java中的静态成员

java - 使用泛型定义 HashMap 的递归定义

java - 获取一个ArrayList

java - 在同一个项目中同时进行 Java 和 Scala 开发

java - 如何使用 Java 以编程方式将 MouseEvent 触发到 MouseListener?