62,614
社区成员
发帖
与我相关
我的任务
分享
Map map1 = new HashMap();
Map map2 = new HashMap();
Map map3 = new HashMap();
Map map4 = new HashMap();
Map map5 = new HashMap();
map1.put("year", 2013);
map1.put("class", "No1");
map1.put("student","zhangsan");
map2.put("year", 2013);
map2.put("class", "No1");
map2.put("student","lisi");
map3.put("year", 2013);
map3.put("class", "No2");
map3.put("student","wangwu");
map4.put("year", 2012);
map4.put("class", "No1");
map4.put("student","zhaoliu");
map5.put("year", 2012);
map5.put("class", "No1");
map5.put("student","tianqi");
List<Map> list = new ArrayList<Map>();
list.add(map1);
list.add(map2);
list.add(map3);
list.add(map4);
list.add(map5);
Map result = new HashMap();
List<Map> dbMapList = list;
List<Map> resultMapList = new ArrayList<Map>();
String s = new String();
for(Map dbMap : dbMapList) {
Map existMap = null;
for(Map resultMap : resultMapList) {
int newYear = (Integer) resultMap.get("year");
int oldYear = (Integer) dbMap.get("year");
if(newYear == oldYear) {
existMap = resultMap;
break;
}else {
continue;
}
}
Map classMap = new HashMap();
classMap.put("class", dbMap.get("class"));
if(existMap != null) {
List l = (List) existMap.get("ClassList");
l.add(classMap);
}else {
Map mapTmp = new HashMap();
mapTmp.put("year", dbMap.get("year"));
List<Map> classMapList = new ArrayList<Map>();
classMapList.add(classMap);
mapTmp.put("ClassList", classMapList);
resultMapList.add(mapTmp);
}
}
result.put("info", resultMapList);
JSONObject json=JSONObject.fromObject(result);
System.out.println(json.toString());