【问题标题】:Grouping similar items using hashmap使用 hashmap 对相似的项目进行分组
【发布时间】:2013-10-08 03:30:32
【问题描述】:

我有这样的输入

0    [0.327097, 0.326998, 0.0]
0    [1.056364, 0.601873, 0.0]
0    [1.273154, 1.656441, 0.0]
1    [1.48469, 0.095074, 0.0]
1    [1.061504, -0.768175, 1.0]

我需要将它们排序为

0 :  [ [0.327097, 0.326998, 0.0] ,[1.056364, 0.601873, 0.0], [1.273154, 1.656441, 0.0]]
1 :  [ [1.48469, 0.095074, 0.0], [1.061504, -0.768175, 1.0]]

我确实喜欢这个..

但我没有得到相同的输出。我的输出重复了。

你能帮帮我吗...

Map<String, Collection<String>> groupMap = new HashMap<String, Collection<String>>();
                   String[] subparts = finalline.split("\\[");    

                  String groupKey;
                  String value;

                  if (subparts.length == 1) {                                 
                      groupKey = null;
                      value = subparts[0];
                  }
                  else    if (subparts.length == 2) {                         
                      groupKey = subparts[0];
                      value = subparts[1];
                  }
                      else {
                      throw new IllegalArgumentException("Can not parse string");
                  }

                  Collection<String> groupContents = groupMap.get(groupKey);     
                  if (groupContents == null) {                                    
                      groupMap.put(groupKey, groupContents = new ArrayList<String>());
                  }
                  groupContents.add(value);                                    

                }

【问题讨论】:

    标签: java iterator hashmap


    【解决方案1】:

    groupMap 映射的值是另一个集合,因此您可以在外部循环中遍历该集合,如下所示

    Map<String, Collection<String>> groupMap = new HashMap<String, Collection<String>>();
    for(String key : groupMap.keySet()){
        System.out.println("Key: " + key);
        Collection<String> values =  groupMap.get(key);
        for(String value : values){
            System.out.println("value: " + value);
        }
    }
    

    【讨论】:

      【解决方案2】:
      Map<String, Collection<String>> groupMap = new HashMap<String, Collection<String>>();
      for (String s : groupMap.keySet()) {
         for (String s1 : groupMap.get(s)) {
             System.out.println(s1);
         }
      }
      

      集合中的集合只是意味着嵌套循环——就像二维数组一样。

      【讨论】:

        【解决方案3】:

        我建议改用来自“Guava”的HashMultimap

        它有助于轻松处理从键到多个值的映射,并且是一种将键与任意多个值关联的通用方法。

        这是一个例子。

        Multimap<String, String> map = HashMultimap.create();
        map.put("1", "a");
        map.put("1", "b");
        
        map.put("2", "c");
        map.put("2", "d");
        

        现在您可以使用“values()”视图来迭代地图中的值。

        for(String value : map.values()) {
            System.out.println(value);
        }
        

        这会给你

        a
        b 
        c 
        d
        

        或者如果你想要键和值,那么你可以使用“entries()”视图。

        for(Map.Entry<String, String> entry : map.entries()) {
            System.out.println("Key: " + entry.getKey() + " Value : " + entry.getValue());
        }
        

        这会给你

        Key: 1 Value : a
        Key: 1 Value : b
        Key: 2 Value : c
        Key: 2 Value : d
        

        如果您正在寻找一个普通的老式 java 简单解决方案

        Map<String, List<String>> map = new HashMap<String, List<String>>();
        // ... Some code to put values in the map
        for(String key : map.keySet()){
            System.out.println("\nKey: " + key);
            List<String> values = map.get(key);
            for(String value : values) {
                System.out.println("Value: " + value);
            }
        }
        

        【讨论】:

          【解决方案4】:

          迭代地图条目的最佳和最有效的方法是:

          Map<String, Collection<String>> map;
          for (Map.Entry<String, Collection<String>> entry : map.entrySet()) {
              System.out.print(entry.getKey()+":");
              for (String str : entry.getValue())
                 System.out.println(str);
          }
          

          此代码将产生您请求的输出。

          请注意,在任何时候都不会查找键。当您遍历条目集时,您可以直接访问(键入的)键和(键入的)值。

          【讨论】:

            【解决方案5】:

            循环遍历Map 中的条目的最有效方法如下:

            Map<String, Collection<String>> groupMap = new HashMap<String, Collection<String>>();
            for (Map.Entry<String, Collection<String>> entry : map.entrySet()) {
                System.out.println("Key: "+entry.getKey());
                for (String val : values) {
                    System.out.printlnln("Value: "+entry.getValue());
                }
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2016-10-28
              • 2014-07-23
              • 1970-01-01
              • 2015-02-23
              • 2016-01-13
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多