Alexander G
Alexander G

Reputation: 1

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

Upvotes: 0

Views: 913

Answers (1)

Jilliss
Jilliss

Reputation: 117

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

}

Upvotes: 1

Related Questions