对集合中的对象分组,并排序

// 数据源
List<ReportStatisticsAgentInfo> list = reportReceiveTaskMapper.getResourceAgentReportTypequarter(param);
if(null==list || list.size()==0){
return result;
}
// 定义一个map 用于分组
Map<String, List<ReportStatisticsAgentInfo>> mapList = new HashMap<String, List<ReportStatisticsAgentInfo>>();
// 遍历集合以AgentId为键,map.get(key)为值保存到集合中
for (Iterator it = list.iterator(); it.hasNext();) {
// 将循环读取的结果放入对象中
ReportStatisticsAgentInfo rsai = (ReportStatisticsAgentInfo) it.next();
// 如果在这个map中包含有相同的键,这创建一个集合将其存起来
if (mapList.containsKey(rsai.getAgentId())) {
List<ReportStatisticsAgentInfo> syn = mapList.get(rsai.getAgentId());
syn.add(rsai);
// 如果没有包含相同的键,在创建一个集合保存数据
} else {
List<ReportStatisticsAgentInfo> syns = new ArrayList<ReportStatisticsAgentInfo>();
syns.add(rsai);
mapList.put(rsai.getAgentId(), syns);
}
}
/**
 * 遍历Map<String, List<ReportStatisticsAgentInfo>> mapList
 * 获取每一个List<ReportStatisticsAgentInfo>
 * 对List<ReportStatisticsAgentInfo>中的元素按照时间排序
 */
for (Map.Entry<String, List<ReportStatisticsAgentInfo>> m : mapList.entrySet()) {
List<ReportStatisticsAgentInfo> rslist = m.getValue();
int tot = 0;
Collections.sort(rslist, new Comparator<ReportStatisticsAgentInfo>() {
@Override
public int compare(ReportStatisticsAgentInfo o1, ReportStatisticsAgentInfo o2) {
/*Long i = o1.getDayTime().getTime() - o2.getDayTime().getTime();*/
String[] s1 = o1.getStrDayTime().split("-");
int l1 = s1.length;
String[] s2 = o1.getStrDayTime().split("-");
int l2 = s2.length;
int i = Integer.parseInt(s1[l1-1])-Integer.parseInt(s2[l2-1]);
if (i > 0) {
return 1;
} else if (i < 0) {
return -1;
} else {
return 0;
}
}
});
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值