group by java实现

时间:2021-12-31 17:48:28

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());
}


}