Reversing a HashMap from Map to Map>

后端 未结 4 1116
遥遥无期
遥遥无期 2021-01-19 04:11

Is there a more elegant/built-in way to reverse the keys and values of a Hashmap?

I currently have the following.

private Map

        
4条回答
  •  爱一瞬间的悲伤
    2021-01-19 04:23

    Guava's BiMap already provides a method for reversing its key-value pairs. Perhaps you could change the interface of the Map in question to BiMap, or else use the following code:

    private BiMap reverseMap(Map permissions) {
       BiMap bimap = HashBiMap.create(permissions);
       return bimap.inverse();
    }
    

提交回复
热议问题