当前位置: 首页 > 知识库问答 >
问题:

如何将一个哈希映射列表中的不同值添加到另一个哈希映射列表中

夏法
2023-03-14

有人能帮我找到一份没有重复的正确清单吗。

我有一个哈希映射列表,比如“HashMap map”,它的大小是4。键值对类似于以下内容

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

我想创建另一个Hashmap列表,其中包含“uri\u 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);
            }
        }

共有3个答案

阮昊阳
2023-03-14

你似乎很困惑,但这会有所帮助。

为了删除重复项并更新参数,您需要从简单字符串更改processedmap-

 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());
             }


        }
鲁烨熠
2023-03-14

问题是,

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循环变量)中获取值并对其进行操作。并且processedMap对于每次运行都将为空,并且需要删除。

方宜
2023-03-14

请尝试以下代码:

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))

在循环内初始化processedMap时始终返回false。

 类似资料:
  • 输入是一个哈希映射,比如 我想写一个方法,返回类型A的列表,其中有键,值属性与字符串类型,和键值从hashmap。 如何让它成为现实?

  • 问题内容: 我目前正在尝试制作一个将动词与西班牙语共轭的程序。我创建了一个哈希表,其中包含一个键和对象Ve​​rb的实例化。键是具有动词不定式形式的字符串(例如“ hablar”)。这是到目前为止我对哈希映射的代码: HashMap中每个动词的键都基于动词的不定式形式。例如,字符串“ hablar”是西班牙语动词的键。Verb类具有一个名为getInfinitive()的方法,该方法返回一个字符串

  • 问题内容: [‘a’,’a’,’b’,’c’,’c’,’c’] 至 和 问题答案: x=[‘a’,’a’,’b’,’c’,’c’,’c’] >>> map(x.count,x) [2, 2, 1, 3, 3, 3] >>> dict(zip(x,map(x.count,x))) {‘a’: 2, ‘c’: 3, ‘b’: 1} >>>

  • 我试图将一个键的值链接到另一个键的值,但似乎无法使其工作。 例如,如果我创建一个哈希映射,我希望能够创建一个键值对,比如(“x”,0)。然后,我希望能够将另一个键值对值添加到第一个键值对中,因此,如果我有(“x”,map.get(“y”))和(“y”,0),我希望能够以某种方式将其链接起来,这样,如果我现在更新y,使其(“y”,10),map。得到(“x”);也应该返回10。 我不知道如何让这个工

  • 本文向大家介绍Java中并发哈希映射和同步哈希映射之间的区别,包括了Java中并发哈希映射和同步哈希映射之间的区别的使用技巧和注意事项,需要的朋友参考一下 并发Hashmap是jdk1.5中引入的类。并发哈希映射仅在添加或更新映射时在称为片段的存储桶级别应用锁。因此,并发哈希映射允许对映射进行并发读写操作。  同步hashmap(Collection.syncronizedHashMap())是C

  • 通常,当哈希映射中的同一个键有多个值时,我找到的答案将使用列表或数组列表来存储值部分。例如: 我想知道是否可以保留hashmap的正常定义并将任何新值附加到旧值(逗号分隔)。类似于这样: } 前面的代码是填充hashmaps,接下来读取单个值时,使用拆分(“,”)并将它们存储在字符串数组中。类似于: