【问题标题】:How to add distinct values form one list of hash map to another list of hash map如何将一个哈希映射列表中的不同值添加到另一个哈希映射列表
【发布时间】:2014-08-21 19:00:08
【问题描述】:

有人可以帮助我获得正确的列表,没有重复。

我有一个哈希映射列表,说“哈希映射映射”,其大小为 4。 键值对类似于下面的内容

("uri_path","/shophome/index")
("AvgResp","30.00")
("count", "3");
("status","200");

我想创建另一个 Hashmap 列表,其中包含“uri_path”的单个条目以及相应计算的平均值和计数。这就是我正在尝试的。理想情况下,新列表的大小应该小于原始列表。有人可以帮助理解它是否出错了

HashMap<String, String> processedMap = null;
        List<HashMap<String, String>> artProcessedEvents=new ArrayList<HashMap<String, String>>();
        for (HashMap<String, String> map : artEvents) {// list that contains the values
            processedMap = new HashMap<String, String>();

            String uri_path= map.get("uri_path");
            Double art = Double.parseDouble(map.get("avgRespT"));   
            count =Integer.parseInt(map.get("count"));



            if (processedMap.containsValue(uri_path)){

                Double artFromMap = Double.parseDouble(processedMap.get("avgRespT"));
                int countFromMap =Integer.parseInt(processedMap.get("count"));
                count = count+countFromMap;
                art = ((art*count) + (artFromMap*countFromMap))/count;

            }

            processedMap.put("uri_path",uri_path);
            processedMap.put("avgRespT",String.valueOf(art));
            processedMap.put("count", String.valueOf(count));
            artProcessedEvents.add(processedMap);
            }
        }

【问题讨论】:

  • 你怎么知道有问题?
  • 这一行:if (processedMap.containsValue(uri_path)){ 没有意义,因为地图总是空的。你刚刚创建了它。

标签: java list hashmap


【解决方案1】:

试试这个代码:

List<HashMap<String, String>> artEvents = ...;

//this map stores all of the result maps (one map per uri_path, uri_path is the key in this uniqueUriMap, map itself being a value).
HashMap<String, HashMap<String, String>> uniqueUriMap = new HashMap<String, HashMap<String, String>>(); 

for (HashMap<String, String> mapBeingProcessed : artEvents) 
{
    String uri_path = mapBeingProcessed.get("uri_path");
    Double art = Double.parseDouble(mapBeingProcessed.get("avgRespT"));   
    Integer count =Integer.parseInt(mapBeingProcessed.get("count"));

    if (uniqueUriMap.containsKey(uri_path)) 
    {
        Double artFromMap = Double.parseDouble(uniqueUriMap.get(uri_path).get("avgRespT"));
        int countFromMap = Integer.parseInt(uniqueUriMap.get(uri_path).get("count"));
        count = count + countFromMap;
        art = ((art * count) + (artFromMap * countFromMap)) / count;
    }

    HashMap<String, String> newUriMap = new HashMap<String, String>();
    newUriMap.put("uri_path",uri_path);
    newUriMap.put("avgRespT",String.valueOf(art));
    newUriMap.put("count", String.valueOf(count));
    uniqueUriMap.put(uri_path, newUriMap);
}
//result list.
List<HashMap<String, String>> artProcessedEvents=new ArrayList<HashMap<String, String>>();
artProcessedEvents.addAll(uniqueUriMap.values());

问题在于存储临时值。这个:

if (processedMap.containsValue(uri_path))

当您在循环内初始化处理映射时,总是返回 false。

【讨论】:

  • 我明白你的意思了,让我试一试并确认谢谢你的代码sn-p
  • 没问题,很高兴能帮上忙。抱歉,描述很少,但这个列表地图地图的东西在评论中描述起来非常复杂。我让代码自己说话;)
  • 我尝试过您提到的类似方式,但它没有进入条件 if (uniqueUriMap.containsValue(uri_path)) 我的要求是,如果我得到另一个具有相同 uri_path 的条目,我应该找到两者的平均值,具体取决于伯爵。例如,如果初始列表仅包含 2 个条目 ("uri_path","/shophome/index") ("AvgResp","30.00") ("count", "3"); (“状态”,“200”); ;
  • ("uri_path","/shophome/index") ("AvgResp","10.00") ("count", "1"); ("status","400") 那么我的最终列表应该只包含 ("uri_path","/shophome/index") ("AvgResp","25.00") ("count", "4");
  • 嘿,我把它改正了。条件应该是 .containsKey 而不是 .containsValue 。非常感谢您的建议它有效:)
【解决方案2】:

问题来了,

if (processedMap.containsValue(uri_path)){

    Double artFromMap = Double.parseDouble(processedMap.get("avgRespT"));
    int countFromMap =Integer.parseInt(processedMap.get("count"));
    count = count+countFromMap;
    art = ((art*count) + (artFromMap*countFromMap))/count;

}

您必须从“map”(for 循环变量)中获取值并对其进行操作。而且每次运行时也处理过的Map 将是空的,需要将其删除。

【讨论】:

    【解决方案3】:

    您似乎在这里很困惑,但这会有所帮助。

    为了删除重复项并更新您的参数,您需要将处理的映射从一个简单的字符串->字符串哈希映射更改为一个字符串->哈希映射,即如下所示。之后您需要通过一个将其添加到列表中迭代器。

     HashMap<String, HashMap<String, String>> processedMap = new HashMap<String, HashMap<String, String>>();
            List<HashMap<String, String>> artProcessedEvents=new ArrayList<HashMap<String, String>>();
            for (HashMap<String, String> map : artEvents) {// list that contains the values
    
    
                String uri_path= map.get("uri_path");
                Double art = Double.parseDouble(map.get("avgRespT"));   
                count =Integer.parseInt(map.get("count"));
    
    
    
                if (processedMap.containsValue(uri_path)){
    
                    Double artFromMap = Double.parseDouble(processedMap.get("avgRespT"));
                    int countFromMap =Integer.parseInt(processedMap.get("count"));
                    count = count+countFromMap;
                    art = ((art*count) + (artFromMap*countFromMap))/count;
    
                }
               //Create a new Hashmap 
                 HashMap<String, String> updatedMap=new HashMap<String, String>();
                updatedMap.put("uri_path",uri_path);
                updatedMap.put("avgRespT",String.valueOf(art));
                updatedMap.put("count", String.valueOf(count));
                processedMap.put(uri_path,updateMap);
    
                }
                //Iterate and add elements to the list
                  for (Map.Entry<String, HashMap<String, String>> entry : processedMap.entrySet())
                 {
                   artProcessedEvents.add(entry.getValue());
                 }
    
    
            }
    

    【讨论】:

      【解决方案4】:

      感谢 Michal 和其他人的建议

      下面的代码解决了我的问题

      List<HashMap<String, String>> artEvents =new ArrayList<HashMap<String, String>>();
      //this map stores all of the result maps (one map per uri_path, uri_path is the key in this uniqueUriMap, map itself being a value).
      HashMap<String, HashMap<String, String>> uniqueUriMap = new HashMap<String, HashMap<String, String>>(); 
      
      for (HashMap<String, String> mapBeingProcessed : artEvents) 
      {
          String uri_path = mapBeingProcessed.get("uri_path");
          Double art = Double.parseDouble(mapBeingProcessed.get("avgRespT"));   
          Integer count =Integer.parseInt(mapBeingProcessed.get("count"));
      
          if (uniqueUriMap.containsKey(uri_path)) 
          {
              Double artFromMap = Double.parseDouble(uniqueUriMap.get(uri_path).get("avgRespT"));
              int countFromMap =Integer.parseInt(uniqueUriMap.get(uri_path).get("count"));
              count = count + countFromMap;
              art = ((art * count) + (artFromMap * countFromMap)) / count;
          }
      
              HashMap<String, String> newUriMap = new HashMap<String, String>();
              newUriMap.put("uri_path",uri_path);
              newUriMap.put("avgRespT",String.valueOf(art));
              newUriMap.put("count", String.valueOf(count));
              uniqueUriMap.put(uri_path, newUriMap);
      
      }
      //result list.
      List<HashMap<String, String>> artProcessedEvents=new ArrayList<HashMap<String, String>>();
      artProcessedEvents.addAll(uniqueUriMap.values()); 
      

      【讨论】:

        猜你喜欢
        • 2013-11-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-13
        • 1970-01-01
        • 2019-05-31
        • 1970-01-01
        相关资源
        最近更新 更多