反转地图|值--->键

问题描述 投票:0回答:1

我希望能够逆转具有多个键的给定HashMap可以指向相同的值。

HashMap<String, String> cities = new HashMap<String, String>();

        cities.put("Manchester", "UK");
        cities.put("London", "UK");
static HashMap<String, String> inverseMap(HashMap map) {
       // something that has "UK" point to both "Manchester" and "London"

      // if it can be done without using any special Java 8 feature, that would be great
}

我不确定从哪里开始。

java dictionary inverse
1个回答
0
投票

您可以查看MultiMap。这是在Google Guava和Apache Commons Collections中都实现的]

它允许将单个键映射到多个值

© www.soinside.com 2019 - 2024. All rights reserved.