group by java实现

public static void abc(List list,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);
}
}
for(Map.Entry<String,List<Object>> entry : tMap.entrySet()){
int billAmount = 0;
TestPolNo polNo = new TestPolNo();
System.out.println("Key = " + entry.getKey());
for(Object t : entry.getValue()){
System.out.println(t.toString());
billAmount+= t.getClass().getDeclaredField("bill").getInt(t);
//t.getBill();
}
polNo.setPolNo(entry.getKey());
polNo.setBillAmount(billAmount);
System.out.println(polNo.toString());
}


}

posted on 2016-12-28 00:37  sunshineicy  阅读(1490)  评论(0编辑  收藏  举报

导航