HashMap重复密钥替换java
我要做的是,假设HashMap>有“11,name2,name1”和“14,name4,name5”…所以我尝试先删除11,它的所有数据name2和name1将被传输到14。但问题是14自己的原始数据名称4和名称5被覆盖。我想把它们都放在14个房间里。我该怎么做?希望你能帮我。多谢各位HashMap重复密钥替换java,java,hashmap,Java,Hashmap,我要做的是,假设HashMap>有“11,name2,name1”和“14,name4,name5”…所以我尝试先删除11,它的所有数据name2和name1将被传输到14。但问题是14自己的原始数据名称4和名称5被覆盖。我想把它们都放在14个房间里。我该怎么做?希望你能帮我。多谢各位 List<String> oldValue = map.remove(oldKey); map.put(newKey, oldValue); List oldValue=map.remove(old
List<String> oldValue = map.remove(oldKey);
map.put(newKey, oldValue);
List oldValue=map.remove(oldKey);
map.put(newKey,oldValue);
您可以尝试以下方法。从旧密钥中提取旧列表。并将列表项添加到新密钥的列表中
// remove old key
List<String> oldValue = map.remove(oldKey);
// add new key with empty list if not exists
if(!map.containsKey(newKey))
{
map.put(newKey, new LinkedList<String>());
}
// new list for new value
List<String> newValue = map.get(newKey);
// add items from old value to new list
for(String s : oldValue){
// eliminate possible duplicates
if(!newValue.contains(s)){
newValue.add(s);
}
}
//删除旧密钥
List oldValue=map.remove(oldKey);
//添加带有空列表的新密钥(如果不存在)
如果(!map.containsKey(newKey))
{
put(newKey,newlinkedlist());
}
//新值的新列表
List newValue=map.get(newKey);
//将项目从旧值添加到新列表
for(字符串s:oldValue){
//消除可能的重复项
如果(!newValue.contains)){
新增价值;
}
}