groupBy
public List groupBy(List list,String flag,String... sortName) throws Exception{
Map<String,List<Object>> tMap = new HashMap<String,List<Object>>();
for(Object t : list){
String filedKey ="";
for(String filedName : sortName){
Field field = t.getClass().getDeclaredField(filedName);
filedKey = field.get(t)+","+filedKey;
System.out.println("filedName======"+filedKey);
}
if(tMap.containsKey(filedKey)){
tMap.get(filedKey).add(t);
}else{
List tList1 = new ArrayList();
tList1.add(t);
tMap.put(filedKey,tList1);
}
}
Field fields[]=list.get(0).getClass().getDeclaredFields();//获得对象所有属性
Field field=null;
for (int i = 0; i < fields.length; i++) {
field=fields[i];
field.setAccessible(true);
}
List<CoastClaimResult> claimList = new ArrayList<CoastClaimResult>();
for(Map.Entry<String,List<Object>> entry : tMap.entrySet()){
BigDecimal billedAmt=new BigDecimal(0);
List<String> distinctCountList = new ArrayList<String>();
CoastClaimResult result = new CoastClaimResult();
System.out.println("Key = " + entry.getKey());
for(Object t : entry.getValue()){
System.out.println(t.toString());
String bill = (String) t.getClass().getDeclaredField("amount"+flag).get(t);
billedAmt.add(new BigDecimal(bill));
String claimNo = (String) t.getClass().getDeclaredField("claimNo").get(t);
if(!distinctCountList.contains(claimNo)){
distinctCountList.add(claimNo);
}
/*String paid = (String) t.getClass().getDeclaredField("amountpaid").get(t);
paidAmt.add(new BigDecimal(paid));*/
}
for(String filedName : sortName){
Field f = CoastClaimResult.class.getClass().getDeclaredField("filedName");
f.setAccessible(true);
f.set(CoastClaimResult.class, (String) list.get(0).getClass().getDeclaredField("filedName").get(list.get(0)));
}
result.setExt1(new BigDecimal(distinctCountList.size()));
if(flag.equals("billed")){
result.setBilledAmt(billedAmt);
result.setTitleCategory("BILLED");
}else{
result.setPaidAmt(billedAmt);
result.setTitleCategory("PAID");
}
// result.setPaidAmt(paidAmt);
claimList.add(result);
// System.out.println(polNo.toString());
}
return claimList;
}
posted on 2016-12-28 18:04 sunshineicy 阅读(180) 评论(0) 编辑 收藏 举报