81,094
社区成员
发帖
与我相关
我的任务
分享
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);
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);
}
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"));
}
}
}
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"));
}
}
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);
Map<String,List> map = new HashMap<String,List>();
if(map.get(key) == null)
{
map.put(key , new ArrayList());
}
map.get(key).add(value);
Map<String,List> = new HashMap<String,List>();