在Java中修改HashMap的现有键 [英] Modify existing Key of HashMap In Java
问题描述
几天以来我一直在使用HashMap,并且面临着以下奇怪的情况.
I'm working with HashMap since few days, and facing below weird situation.
案例1:已更改的密钥(已存在于HashMap中),并打印HashMap情况2:更改了HashMap中已经存在的密钥,然后将该密钥再次放入HashMap中.打印HashMap.
Case 1:Changed Key which is already existing in HashMap, and print HashMap Case 2: Changed key which is already existing in HashMap and Put that key again into the HashMap. Print HashMap.
请找到以下代码以及两种情况下的两种不同输出.
Please find below code as well as two different output of two case.
请让任何人让我知道,下面的代码中发生了什么.
Could you please anyone let me know, whats going on in below code.
import java.util.HashMap;
import java.util.Set;
class Emp{
private String id ;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Emp(String id) {
super();
this.id = id;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Emp other = (Emp) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
@Override
public String toString() {
return "Emp [id=" + id + "]";
}
}
public class HashMapChanges {
public static void main(String[] args) {
// TODO Auto-generated method stub
Emp e1 = new Emp("1");
Emp e2 = new Emp("2");
Emp e3 = new Emp("3");
Emp e4 = new Emp("4");
HashMap<Emp, String> hm = new HashMap<Emp,String>();
hm.put(e1,"One");
hm.put(e2,"Two");
hm.put(e3,"Three");
hm.put(e4,"Four");
e1.setId("5");
/** Uncomment below line to get different output**/
//hm.put(e1,"Five-5");
Set<Emp> setEmpValue = hm.keySet();
for(Emp e : setEmpValue){
System.out.println("Key"+ e +" Value "+ hm.get(e));
}
}
}
以上代码的输出:
KeyEmp [id=2] Value Two
KeyEmp [id=5] Value null
KeyEmp [id=4] Value Four
KeyEmp [id=3] Value Three
在注释行后输出
KeyEmp [id=5] Value Five-5
KeyEmp [id=2] Value Two
KeyEmp [id=5] Value Five-5
KeyEmp [id=4] Value Four
KeyEmp [id=3] Value Three
推荐答案
当用于确定其在Map中位置的键是可变的时,不允许将可变对象用作Map中的键.
Using mutable objects as keys in a Map is not permitted when the key used to determine its location in the Map is mutable.
从Javadoc中获取 java.util.Map< K,V>
:
From the Javadoc for java.util.Map<K,V>
:
注意:如果将可变对象用作地图键,则必须格外小心.如果在对象是映射中的键的情况下以影响等值比较的方式更改对象的值,则不会指定映射的行为.
Note: great care must be exercised if mutable objects are used as map keys. The behavior of a map is not specified if the value of an object is changed in a manner that affects equals comparisons while the object is a key in the map.
由于您的 Emp
对象是可变的,因此您违反了地图键所需的约定,并且更改修改了用于确定键在地图中所处位置的属性.
You are violating the contract required of map keys because your Emp
object is mutable, and the change modifies the attribute used to determine where in the map the key resides.
结果是未定义的行为.
我怀疑您根据您的代码误解了 Map
概念,但在不了解您实际要实现的目标的情况下,我们真的无能为力.我建议您提出一个新问题,解释您的实际目标.
I suspect you have misunderstood the Map
concept, based on your code, but without understanding what you're actually trying to achieve we really cannot help further. I suggest you ask a new question explaining your actual goals.
这篇关于在Java中修改HashMap的现有键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!