# java 集合分组求和

0

AB  A  9.0  4.0

AB  B  7.0  5.0
AB  C  4.0  3.0
AB  D  8.0  2.0
AB合计  28.0  14.0
CD  A  7.0  3.0
CD  B  5.0  1.0
CD合计  12.0  4

package com.test;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Test1 {
public static void main(String[] args) {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
List<Map<String, Object>> xlist=groupCount(list);
for (Map<String, Object> map : xlist) {
System.out.println(map);
}
}

public static List<Map<String, Object>> groupCount(List<Map<String, Object>> list){
List<Map<String, Object>> xList =new ArrayList<Map<String,Object>>();
if(list!=null&&list.size()>0){
double zhj1=0,gdzhj1=0;
for(int i=1;i<list.size();i++){
int size=xList.size()-1;
Map<String,Object> map=list.get(i);
Map<String,Object> map1=xList.get(size);
String xz = object2String(map.get("乡镇"));
String xz1 = object2String(map1.get("乡镇"));
double zj = object2Double(map.get("总计"));
double gdzj = object2Double(map.get("耕地"));
if(xz.equals(xz1)){
double zj1 = object2Double(map1.get("总合计"));
double gdzj1 = object2Double(map1.get("耕地"));
Map<String, Object> map2=new HashMap<String, Object>();
map2.put("乡镇总合计", xz+"总合计");
map2.put("村", null);
map2.put("总计",zhj);
map2.put("耕地",gdzhj);
}else{
}
}
Map<String, Object> map=new HashMap<String, Object>();
map.put("所有总计","所有总计");
map.put("总计", zhj1);
map.put("耕地", gdzhj1);
}

return xList;
}

public static double add(double v1, double v2) {
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
}

public static String object2String(Object obj){
return obj == null ? "" : obj.toString();
}
public static double object2Double(Object obj){
String value = object2String(obj);
return "".equals(value) ? 0 : Double.valueOf(value);
}
public static Map<String, Object> createMap(String town,String village,double zjmj,double gdmj){
Map<String, Object> map = new HashMap<String, Object>();
map.put("乡镇", town);
map.put("村", village);
map.put("总计",zjmj);
map.put("耕地", gdmj);
return map;
}
}

您需要登录以后才能回答，未注册用户请先注册