Java 不可变对象和哈希映射键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20212440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Immutable objects and hashmap keys
提问by a Learner
Are immutable objects (other than String
like Integer
and other wrapper classes etc.) good for hashmap keys?
不可变对象(除了String
喜欢Integer
和其他包装类等)是否适合哈希映射键?
Can anybody explain how?
有人能解释一下怎么做吗?
采纳答案by Renjith
If immutable, the object's hashcode wont change and it allows caching the hashcode of different keys which makes the overall retrieval process very fast. Also for mutable objects ,the hashCode() might be dependent on fields that could change, if this happens you wont be able to find the key (and its value) in the HashMap since hashCode() returns different value.
如果不可变,对象的哈希码不会改变,它允许缓存不同键的哈希码,这使得整个检索过程非常快。同样对于可变对象,hashCode() 可能依赖于可能更改的字段,如果发生这种情况,您将无法在 HashMap 中找到键(及其值),因为 hashCode() 返回不同的值。
回答by Thomas
If you object is immutable and implements hashcode/equals correctly, you are fine to use them as keys in a hashmap.
如果您的对象是不可变的并且正确实现了 hashcode/equals,则可以将它们用作 hashmap 中的键。
回答by shreyansh jogi
yes because it is unchangeable.
是的,因为它是不可改变的。
Lets assume that i am having one class
让我们假设我有一节课
MyKey key = new MyKey("shreyansh"); //assume hashCode=1234
myHashMap.put(key, "value");
// Below code will change the key hashCode() and equals()
// but it's location is not changed.
key.setName("jogi"); //assume new hashCode=7890
//below will return null, because HashMap will try to look for key
//in the same index as it was stored but since key is mutated,
//there will be no match and it will return null.
myHashMap.get(new MyKey("shreyansh"));
here while accessing that using key "Shreyansh" it will return nulll
在这里使用密钥“Shreyansh”访问它时,它将返回 nulll
回答by Hymany
You can find the answer here: How HashMap works in Java
您可以在这里找到答案:HashMap 在 Java 中的工作原理
String, Integer and other wrapper classes are natural candidates of HashMap key, and String is most frequently used key as well because String is immutable and final,and overrides equals and hashcode() method. Other wrapper class also shares similar property. Immutabiility is required, in order to prevent changes on fields used to calculate hashCode() because if key object return different hashCode during insertion and retrieval than it won't be possible to get object from HashMap. Immutability is best as it offers other advantages as well like thread-safety, If you can keep your hashCode same by only making certain fields final, then you go for that as well. Since equals() and hashCode() method is used during retrieval of value object from HashMap, its important that key object correctly override these methods and follow contact. If unequal object return different hashcode than chances of collision will be less which subsequently improve performance of HashMap.
String、Integer 和其他包装类是 HashMap 键的自然候选,而 String 也是最常用的键,因为 String 是不可变的和 final 的,并且覆盖了 equals 和 hashcode() 方法。其他包装类也共享类似的属性。需要不变性,以防止更改用于计算 hashCode() 的字段,因为如果键对象在插入和检索期间返回不同的 hashCode,则无法从 HashMap 获取对象。不变性是最好的,因为它提供了其他优点以及线程安全性,如果您可以通过仅将某些字段设为 final 来保持您的 hashCode 相同,那么您也可以这样做。由于在从 HashMap 检索值对象期间使用了 equals() 和 hashCode() 方法,因此关键对象正确覆盖这些方法并遵循联系非常重要。
There is also another stack for the discussion: Why are immutable objects in hashmaps so effective
还有另一个堆栈可供讨论:为什么哈希映射中的不可变对象如此有效
Both hashcode and equals method are used in put and get method of HashMap. You need to make sure you can always get the value object from the map after you put it with the key object. No matter you change the key object or not. But Immutable object is good enough to achieve that.
HashMap 的 put 和 get 方法中都使用了 hashcode 和 equals 方法。您需要确保在将值对象与键对象放在一起后始终可以从映射中获取值对象。无论您是否更改关键对象。但是不可变对象足以实现这一点。