packagetest;importjavaBean.CustomerVo;importservice.ListSortUtil;importjava.text.ParseException;importjava.text.SimpleDateFormat;import java.util.*;/*** 测试集合分组算法
*
*@author清茶
* @create 2017-10-26 10:40*/
public classTestListSort {public static voidmain(String[] args) {
List list = new ArrayList();
list.add(new CustomerVo(1L, 2000L, "张1", generateDate(2017, 10, 10)));
list.add(new CustomerVo(2L, 2000L, "张2", generateDate(2017, 10, 10)));
list.add(new CustomerVo(3L, 2001L, "张3", generateDate(2017, 11, 10)));
list.add(new CustomerVo(4L, 2001L, "张4", generateDate(2017, 11, 10)));
list.add(new CustomerVo(5L, 2002L, "张5", generateDate(2017, 12, 10)));
list.add(new CustomerVo(6L, 2002L, "张6", generateDate(2017, 12, 10)));//按照客户日期进行排序
Map> sortMapByDate =sortByDate(list);
Map> sortMapByLong =sortByLong(list);
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");for (Map.Entry>entry : sortMapByDate.entrySet()) {
Date key=entry.getKey();
List customerVos =entry.getValue();
System.out.print("键值为:" + sdf.format(key) + "所包含的客户ID分别为:");for(CustomerVo vo : customerVos) {
System.out.print(vo.getId()+ ",");
}
System.out.println();
}
}private static Map> sortByDate(Listvos) {return ListSortUtil.group(vos, new ListSortUtil.GroupBy() {publicDate groupBy(Object obj) {
CustomerVo vo=(CustomerVo) obj;returnvo.getDate();
}
});
}private static Map> sortByLong(Listvos) {return ListSortUtil.group(vos, new ListSortUtil.GroupBy() {publicLong groupBy(Object obj) {
CustomerVo vo=(CustomerVo) obj;returnvo.getId();
}
});
}private static Date generateDate(int year, int month, intday) {
Calendar calendar=Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month- 1);
calendar.set(Calendar.DAY_OF_MONTH, day);
Date date=calendar.getTime();
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
Date returnDate= newDate();try{
System.out.println(sdf.format(date));
returnDate=sdf.parse(sdf.format(date));
}catch(ParseException e) {
e.printStackTrace();
}returnreturnDate;
}
}