有人可以帮助我获得没有重复的正确列表。
我有一个哈希映射列表,说“HashMap map”,其大小为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);
}
}
答案 0 :(得分:0)
这是问题,
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都将为空,并且需要将其删除。
答案 1 :(得分:0)
试试这段代码:
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。
答案 2 :(得分:0)
你似乎在这里混淆了很多,但这会有所帮助。
为了删除重复项并更新参数,您需要将processedmap从简单的string-&gt;字符串hashmap更改为string-&gt; Hashmap,如下所示。接下来,您需要将其添加到列表中通过迭代器。
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());
}
}
答案 3 :(得分:0)
感谢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());