【发布时间】:2018-02-05 01:36:26
【问题描述】:
我有一个枚举类如下。
public enum Item {
COKE("Coke", 25), PEPSI("Pepsi", 35), SODA("Soda", 45);
private String name;
private int price;
private Item(String name, int price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public long getPrice() {
return price;
}
}
和一个hashmap定义如下
private Map<Item, Integer> inventory = new HashMap<Item, Integer>();
我是否需要覆盖枚举Item 类中的哈希码和等于inventory.put() 和inventory.get() 才能正常工作?如果不是,为什么?
【问题讨论】:
-
或者你有没有得到类似stackoverflow.com/questions/40028231/…的东西?
-
还要检查
EnumMap。
标签: java hash hashmap equals hashcode