我希望能够逆转具有多个键的给定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
}
我不确定从哪里开始。
您可以查看MultiMap。这是在Google Guava和Apache Commons Collections中都实现的]
它允许将单个键映射到多个值