小编pro*_*tor的帖子

如何使用值作为列表或数组对 HashMap 中的键进行分组

我使用 String 作为键和 Integer 作为值创建了一个 Map。所以,就像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);
Run Code Online (Sandbox Code Playgroud)

我想根据它们的代码以列表或数组格式获取城市。因此,例如对于 value 43,它应该返回类似{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 …
Run Code Online (Sandbox Code Playgroud)

java hashmap key-value

2
推荐指数
1
解决办法
1万
查看次数

如何使用 Java 中的一些解析器在低内存中解析 csv?

我使用了 InputStream,并且在解析时,如果","在一列中有一个,那么它会将其视为单独的列。ex -abc, xyz, "m,n" 那么解析的输出是abc , xyz, m, n 这里 m 和 n 被视为单独的列。

java csv parsing inputstream memory-efficient

0
推荐指数
1
解决办法
937
查看次数

标签 统计

java ×2

csv ×1

hashmap ×1

inputstream ×1

key-value ×1

memory-efficient ×1

parsing ×1