如何使用值作为列表或数组对 HashMap 中的键进行分组
How to group the keys from a HashMap using values as a List or an Array
我使用字符串作为键并使用整数作为值创建了一个映射。所以,就像 citiesWithCodes
.
出于测试目的,我已经手动将值放入 Hashmap 中。他们是:
Map<String, Integer> citiesWithCodes = new HashMap<String, Integer>();
citiesWithCodes.put("Berlin", 49);
citiesWithCodes.put("Frankfurt", 49);
citiesWithCodes.put("Hamburg", 49);
citiesWithCodes.put("Cologne", 49);
citiesWithCodes.put("Salzburg", 43);
citiesWithCodes.put("Vienna", 43);
citiesWithCodes.put("Zurich", 41);
citiesWithCodes.put("Bern", 41);
citiesWithCodes.put("Interlaken", 41);
我想根据他们的代码以列表或数组格式获取城市。因此,例如对于值 43
,它应该 return 类似于 {43=[Vienna, Salzburg]}
.
我试过下面的方法。这绝对是一种肮脏的方法,并且没有给出正确的结果。
public static Map<Integer, List<String>> codeCities(Map<String, Integer> citiesWithCodes){
Map<Integer, List<String>> segList = new HashMap<Integer, List<String>>();
List<String> city;
Iterator<Entry<String, Integer>> i = citiesWithCodes.entrySet().iterator();
while (i.hasNext()) {
city = new ArrayList<String>();
Entry<String, Integer> next = i.next();
i.remove();
city.add(next.getKey());
for (Entry<String, Integer> e : citiesWithCodes.entrySet()) {
if(e.getValue().equals(next.getValue())){
city.add(e.getKey());
citiesWithCodes.remove(e);
}
}
System.out.println(city);
segList.put(next.getValue(), city);
}
return segList;
}
我得到的输出是:{49=[Cologne], 41=[Interlaken], 43=[Salzburg]}
有人能告诉我,实现结果的正确方法吗?
PS:我知道可以使用 MultiMap。但我们仅限于使用 Java Collection Framework 而不是 Java 8。
您总是构建一个新的 List<String>
来保存具有给定键的城市列表。发生在行
city = new ArrayList<String>();
将其替换为
if(segList.containsKey(next.getValue())) {
city = segList.get(next.getValue());
} else {
city = new ArrayList<String>();
}
并且代码应该按预期工作。
编辑:@eran 速度更快,但我保留了它,因为它解释了不太优雅和现代的方法所犯的错误。
如果您的范围限于 Java 7 然后尝试更改如下代码:
Map<Integer, List<String>> segList = new HashMap<Integer, List<String>>();
Iterator<Entry<String, Integer>> i = citiesWithCodes.entrySet().iterator();
while (i.hasNext()) {
Entry<String, Integer> next = i.next();
if (segList.get(next.getValue()) != null) {
List<String> city= segList.get(next.getValue());
city.add(next.getKey());
segList.put(next.getValue(), city);
}else{
List<String> city=new ArrayList<String>();
city.add(next.getKey());
segList.put(next.getValue(), city);
}
}
输出:
{49=[Frankfurt, Berlin, Hamburg, Cologne], 41=[Bern, Zurich,
Interlaken], 43=[Vienna, Salzburg]}
之前 java 8
package com.Whosebug;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class HashMapToListMap {
public static void main(String[] args) {
Map<String, Integer> citiesWithCodes = new HashMap<String, Integer>();
citiesWithCodes.put("Berlin", 49);
citiesWithCodes.put("Frankfurt", 49);
citiesWithCodes.put("Hamburg", 49);
citiesWithCodes.put("Cologne", 49);
citiesWithCodes.put("Salzburg", 43);
citiesWithCodes.put("Vienna", 43);
citiesWithCodes.put("Zurich", 41);
citiesWithCodes.put("Bern", 41);
citiesWithCodes.put("Interlaken", 41);
Map<Integer, List<String>> result = new HashMap<Integer, List<String>>();
for(Entry<String,Integer> entry : citiesWithCodes.entrySet()){
List<String> list = new ArrayList<String>();
if(result.containsKey(entry.getValue()))
list = result.get(entry.getValue());
list.add(entry.getKey());
result.put(entry.getValue(), list);
}
System.out.println(result);
}
}
java8
之后
package com.Whosebug;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class HashMapToListMap {
public static void main(String[] args) {
Map<String, Integer> citiesWithCodes = new HashMap<String, Integer>();
citiesWithCodes.put("Berlin", 49);
citiesWithCodes.put("Frankfurt", 49);
citiesWithCodes.put("Hamburg", 49);
citiesWithCodes.put("Cologne", 49);
citiesWithCodes.put("Salzburg", 43);
citiesWithCodes.put("Vienna", 43);
citiesWithCodes.put("Zurich", 41);
citiesWithCodes.put("Bern", 41);
citiesWithCodes.put("Interlaken", 41);
Map<Integer, List<String>> result = citiesWithCodes.entrySet().stream().collect(Collectors.groupingBy(
Map.Entry::getValue,Collectors.mapping(Map.Entry::getKey, Collectors.toList())));
System.out.println(result);
}
}
我使用字符串作为键并使用整数作为值创建了一个映射。所以,就像 citiesWithCodes
.
出于测试目的,我已经手动将值放入 Hashmap 中。他们是:
Map<String, Integer> citiesWithCodes = new HashMap<String, Integer>();
citiesWithCodes.put("Berlin", 49);
citiesWithCodes.put("Frankfurt", 49);
citiesWithCodes.put("Hamburg", 49);
citiesWithCodes.put("Cologne", 49);
citiesWithCodes.put("Salzburg", 43);
citiesWithCodes.put("Vienna", 43);
citiesWithCodes.put("Zurich", 41);
citiesWithCodes.put("Bern", 41);
citiesWithCodes.put("Interlaken", 41);
我想根据他们的代码以列表或数组格式获取城市。因此,例如对于值 43
,它应该 return 类似于 {43=[Vienna, Salzburg]}
.
我试过下面的方法。这绝对是一种肮脏的方法,并且没有给出正确的结果。
public static Map<Integer, List<String>> codeCities(Map<String, Integer> citiesWithCodes){
Map<Integer, List<String>> segList = new HashMap<Integer, List<String>>();
List<String> city;
Iterator<Entry<String, Integer>> i = citiesWithCodes.entrySet().iterator();
while (i.hasNext()) {
city = new ArrayList<String>();
Entry<String, Integer> next = i.next();
i.remove();
city.add(next.getKey());
for (Entry<String, Integer> e : citiesWithCodes.entrySet()) {
if(e.getValue().equals(next.getValue())){
city.add(e.getKey());
citiesWithCodes.remove(e);
}
}
System.out.println(city);
segList.put(next.getValue(), city);
}
return segList;
}
我得到的输出是:{49=[Cologne], 41=[Interlaken], 43=[Salzburg]}
有人能告诉我,实现结果的正确方法吗?
PS:我知道可以使用 MultiMap。但我们仅限于使用 Java Collection Framework 而不是 Java 8。
您总是构建一个新的 List<String>
来保存具有给定键的城市列表。发生在行
city = new ArrayList<String>();
将其替换为
if(segList.containsKey(next.getValue())) {
city = segList.get(next.getValue());
} else {
city = new ArrayList<String>();
}
并且代码应该按预期工作。
编辑:@eran 速度更快,但我保留了它,因为它解释了不太优雅和现代的方法所犯的错误。
如果您的范围限于 Java 7 然后尝试更改如下代码:
Map<Integer, List<String>> segList = new HashMap<Integer, List<String>>();
Iterator<Entry<String, Integer>> i = citiesWithCodes.entrySet().iterator();
while (i.hasNext()) {
Entry<String, Integer> next = i.next();
if (segList.get(next.getValue()) != null) {
List<String> city= segList.get(next.getValue());
city.add(next.getKey());
segList.put(next.getValue(), city);
}else{
List<String> city=new ArrayList<String>();
city.add(next.getKey());
segList.put(next.getValue(), city);
}
}
输出:
{49=[Frankfurt, Berlin, Hamburg, Cologne], 41=[Bern, Zurich, Interlaken], 43=[Vienna, Salzburg]}
之前 java 8
package com.Whosebug;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class HashMapToListMap {
public static void main(String[] args) {
Map<String, Integer> citiesWithCodes = new HashMap<String, Integer>();
citiesWithCodes.put("Berlin", 49);
citiesWithCodes.put("Frankfurt", 49);
citiesWithCodes.put("Hamburg", 49);
citiesWithCodes.put("Cologne", 49);
citiesWithCodes.put("Salzburg", 43);
citiesWithCodes.put("Vienna", 43);
citiesWithCodes.put("Zurich", 41);
citiesWithCodes.put("Bern", 41);
citiesWithCodes.put("Interlaken", 41);
Map<Integer, List<String>> result = new HashMap<Integer, List<String>>();
for(Entry<String,Integer> entry : citiesWithCodes.entrySet()){
List<String> list = new ArrayList<String>();
if(result.containsKey(entry.getValue()))
list = result.get(entry.getValue());
list.add(entry.getKey());
result.put(entry.getValue(), list);
}
System.out.println(result);
}
}
java8
之后 package com.Whosebug;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class HashMapToListMap {
public static void main(String[] args) {
Map<String, Integer> citiesWithCodes = new HashMap<String, Integer>();
citiesWithCodes.put("Berlin", 49);
citiesWithCodes.put("Frankfurt", 49);
citiesWithCodes.put("Hamburg", 49);
citiesWithCodes.put("Cologne", 49);
citiesWithCodes.put("Salzburg", 43);
citiesWithCodes.put("Vienna", 43);
citiesWithCodes.put("Zurich", 41);
citiesWithCodes.put("Bern", 41);
citiesWithCodes.put("Interlaken", 41);
Map<Integer, List<String>> result = citiesWithCodes.entrySet().stream().collect(Collectors.groupingBy(
Map.Entry::getValue,Collectors.mapping(Map.Entry::getKey, Collectors.toList())));
System.out.println(result);
}
}