List、、、合并Map问题

绝世酱油瓶 2011-12-23 03:13:56
如果我有如下格式的数据:
		List<Map> list = new ArrayList<Map>();
Map map1 = new HashMap();
map1.put("submitDate", "2011-12-10");
map1.put("scoreValue", 20);
Map map2 = new HashMap();
map2.put("submitDate", "2011-12-10");
map2.put("scoreValue", 10);
Map map3 = new HashMap();
map3.put("submitDate", "2011-12-11");
map3.put("scoreValue", 11);
Map map4 = new HashMap();
map4.put("submitDate", "2011-12-11");
map4.put("scoreValue", 11);
Map map5 = new HashMap();
map5.put("submitDate", "2011-12-12");
map5.put("scoreValue", 5);
list.add(map1);
list.add(map2);
list.add(map3);
list.add(map4);
list.add(map5);

如何高效率的把submitDate对应的value相同的map合并、而且把对应的scoreValue的值累加起来、类似
map1.put("submitDate", "2011-12-10"); map2.put("submitDate", "2011-12-11");
map1.put("scoreValue", 30); map2.put("scoreValue", 22);
。。。。。
...全文
324 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
Sai 2011-12-23
  • 打赏
  • 举报
回复


Map<String, Integer> map = new HashMap<String, Integer>();
for (int i = 0; i < list.size(); i++) {
String date = (String)((Map)list.get(i)).get("submitDate");
int score = (Integer)((Map)list.get(i)).get("scoreValue");
if (map.isEmpty() || !map.containsKey(date)) {
map.put(date, score);
} else {
map.put(date, score + map.get(date));
}
}

List arrayList = new ArrayList();
Set<String> hashSet = map.keySet();
Iterator<String> iterator = hashSet.iterator();
while (iterator.hasNext()) {
String elem = iterator.next();
Map hashMap = new HashMap();
hashMap.put("submitDate", elem);
hashMap.put("scoreValue", map.get(elem));
arrayList.add(hashMap);
}
funfenffun 2011-12-23
  • 打赏
  • 举报
回复

public class Test {
public static void main(String[] args) {
List<Map> list = new ArrayList<Map>();
Map map1 = new HashMap();
map1.put("submitDate", "2011-12-10");
map1.put("scoreValue", 20);
Map map2 = new HashMap();
map2.put("submitDate", "2011-12-10");
map2.put("scoreValue", 10);
Map map3 = new HashMap();
map3.put("submitDate", "2011-12-11");
map3.put("scoreValue", 11);
Map map4 = new HashMap();
map4.put("submitDate", "2011-12-11");
map4.put("scoreValue", 11);
Map map5 = new HashMap();
map5.put("submitDate", "2011-12-12");
map5.put("scoreValue", 5);
list.add(map1);
list.add(map2);
list.add(map3);
list.add(map4);
list.add(map5);

List<Map> list0 = new ArrayList<Map>();
boolean flag = false;
for(Map map : list){
for(Map map0 : list0){
if(map.get("submitDate").equals(map0.get("submitDate"))){
int scoreValue = Integer.parseInt(map0.get("scoreValue").toString());
map0.remove("scoreValue");
map0.put("scoreValue", scoreValue+Integer.parseInt(map.get("scoreValue").toString()));
flag = true;
break;
}
}
if(flag == false){
list0.add(map);
}
flag = false;
}
for(Map map : list0){
System.out.println(map.get("submitDate") + " " + map.get("scoreValue"));
}

}

}

亲努力啊 2011-12-23
  • 打赏
  • 举报
回复

Map map = new HashMap();
for(int i = 0; i < list.size(); i++){
if(map.get(list.get(i).get("submitDate")) == null){
map.put(list.get(i).get("submitDate"), list.get(i).get("scoreValue"));
}else{
map.put(list.get(i).get("submitDate"),
map.get(list.get(i).get("submitDate")) + list.get(i).get("scoreValue"));
}
}
亲努力啊 2011-12-23
  • 打赏
  • 举报
回复
Map map = new HashMap();
int sum = 0;
for(int i = 0; i < list.size(); i++){
if(map.get(list.get(i).get("submitDate")) == null){
map.put(list.get(i).get("submitDate"), list.get(i).get("scoreValue"));
}else{
map.put(list.get(i).get("submitDate"), map.get(list.get(i).get("submitDate")) + list.get(i).get("scoreValue"));
}
}
我嘞个去 2011-12-23
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 java_devil 的回复:]
引用 2 楼 zhang842265223 的回复:

虽然不知道你在说什么,不过帮顶
我的需求是,一个List里有N个Map、每个Map有2个Key(date,score)、如果其中有M个Map的date对应的value相同、那么就把那M个Map合并、但是把他们的score对应的value累加一起作为合并后的那个Map的value值、
[/Quote]
绝世酱油瓶 2011-12-23
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zhang842265223 的回复:]

虽然不知道你在说什么,不过帮顶
[/Quote]我的需求是,一个List里有N个Map、每个Map有2个Key(date,score)、如果其中有M个Map的date对应的value相同、那么就把那M个Map合并、但是把他们的score对应的value累加一起作为合并后的那个Map的value值、
绝世酱油瓶 2011-12-23
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 aaaa8215 的回复:]

Java code

Map<String,List> map = new HashMap<String,List>();
if(map.get(key) == null)
{
map.put(key , new ArrayList());
}
map.get(key).add(value);
[/Quote]
我的需求是,一个List里有N个Map、每个Map有2个Key(date,score)、如果其中有M个Map的date对应的value相同、那么就把那M个Map合并、但是把他们的score对应的value累加一起作为合并后的那个Map的value值、
leilei0932_java 2011-12-23
  • 打赏
  • 举报
回复
试试看是不是你想要的结果:

List<Map> list = new ArrayList<Map>();
Map map1 = new HashMap();
map1.put("submitDate", "2011-12-10");
map1.put("scoreValue", 20);
Map map2 = new HashMap();
map2.put("submitDate", "2011-12-10");
map2.put("scoreValue", 10);
Map map3 = new HashMap();
map3.put("submitDate", "2011-12-11");
map3.put("scoreValue", 11);
Map map4 = new HashMap();
map4.put("submitDate", "2011-12-11");
map4.put("scoreValue", 11);
Map map5 = new HashMap();
map5.put("submitDate", "2011-12-12");
map5.put("scoreValue", 5);
list.add(map1);
list.add(map2);
list.add(map3);
list.add(map4);
list.add(map5);

List<Map> mapList = new ArrayList<Map>();
for (int i = 0; i < list.size(); i++) {
Map m1 = list.get(i);
mapList.add(list.get(i));
for (int j = 0; j < mapList.size(); j++) {
if(i<list.size()-1){
Map m2 = list.get(i+1);
Map m3 = mapList.get(j);
if(m2.get("submitDate").equals(m3.get("submitDate"))){
m2.put("scoreValue", (Integer)m3.get("scoreValue")+(Integer)m2.get("scoreValue"));
mapList.remove(m1);
}
}
}
}

System.out.println(mapList);
aaaa8215 2011-12-23
  • 打赏
  • 举报
回复

Map<String,List> map = new HashMap<String,List>();
if(map.get(key) == null)
{
map.put(key , new ArrayList());
}
map.get(key).add(value);
aaaa8215 2011-12-23
  • 打赏
  • 举报
回复
用泛型map

Map<String,List> = new HashMap<String,List>();
亲努力啊 2011-12-23
  • 打赏
  • 举报
回复
Map map = new HashMap();
int sum = 0;
for(int i = 0; i < list.size(); i++){
map.put(list.get(i).get("submitDate"),"submitDate");
sum = sum + list.get(i).get("scoreValue");
}
AMD战神 2011-12-23
  • 打赏
  • 举报
回复
虽然不知道你在说什么,不过帮顶
dongxiaolei 2011-12-23
  • 打赏
  • 举报
回复
遍历一遍,把相同的放在一个数组里面
Java List去掉重复对象-java8 一、去除List中重复的Stringpublic List removeStringListDupli(List stringList) { Set set = new LinkedHashSet<>(); set.addAll(stringList); stringList.clear(); stringList.addA

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧