How to swap keys and values in a Map elegantly

前端 未结 8 1868
滥情空心
滥情空心 2020-12-25 14:49

I already know how to do it the hard way and got it working - iterating over entries and swapping \"manually\". But i wonder if, like so many tasks, this one can be solved

8条回答
  •  春和景丽
    2020-12-25 15:23

    If you had access to apache commons-collections, you could have used MapUtils.invertMap.

    Note: The behaviour in case of duplicated values is undefined.

    (Replying to this as this is the first google result for "java invert map").

提交回复
热议问题