Reverse HashMap keys and values in Java

前端 未结 8 1814
梦谈多话
梦谈多话 2020-11-27 17:59

It\'s a simple question, I have a simple HashMap of which i want to reverse the keys and values.

HashMap myHashMap = new HashMap<         


        
8条回答
  •  野性不改
    2020-11-27 18:18

    Iterate through the list of keys and values, then add them.

    HashMap reversedHashMap = new HashMap();
    for (String key : myHashMap.keySet()){
        reversedHashMap.put(myHashMap.get(key), key);
    }
    

提交回复
热议问题