简体   繁体   中英

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

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

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); 

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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