简体   繁体   English

如何将 HashMap 中的键替换为 newMap 中的值?

[英]how replace key from HashMap to values in newMap?

HashMap<String,String> map1 = new HashMap<String,String>(); 
map1.put("1","Russia"); 
map1.put("2","USA");

//values from map2 to key in map1 or in newMap //map2中的值到map1或newMap中的键

HashMap<String,String> map2 = new HashMap<String,String>(); 
map2.put("1","Moskow"); 
map2.put("2","New York");

//how relplace value to key? //如何将值替换为键? in new Map在新 Map

HashMap<String,String> newMap = new HashMap<String,String>(); 
newMap.put("New York","USA"); //key "2" from map1 & map2

The way you describe it, it looks like this你描述它的方式,它看起来像这样


Set<String> kyes = map1.keySet();

for(String key: keys){
 String newKey = map2.get(key);
 String newValue = map1.get(key);
 newMap.put(newKey,newValue); 

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM