java对list的常用操作(排序,分组,去重)
原创
©著作权归作者所有:来自51CTO博客作者小鲍侃java的原创作品,请联系作者获取转载授权,否则将追究法律责任
1.去重
public static ArrayList<OrganizationInfoBO> removeDuplicteUsers(ArrayList<OrganizationInfoBO> listOrganizationInfoBo) {
Set<OrganizationInfoBO> setBo = new TreeSet<OrganizationInfoBO>(new Comparator<OrganizationInfoBO>() {
@Override
public int compare(OrganizationInfoBO o1, OrganizationInfoBO o2) {
return o1.getId().compareTo(o2.getId());
}
});
setBo.addAll(listOrganizationInfoBo);
return new ArrayList<OrganizationInfoBO>(setBo);
}
2.分组
Map<String, List<AttendRulesInfoBO>> group = resultBo(list对象).
stream().collect(Collectors.groupingBy
(AttendRulesInfoBO(list中VO)::getLateClass(VO中get方法)));
3.排序
//按照条码类别倒序,条码编码正序排列
Collections.sort(result, new Comparator<BarcodeManageVo>() {
@Override
public int compare(BarcodeManageVo bo1, BarcodeManageVo bo2) {
return bo2.getBatchCode().compareTo(bo1.getBatchCode());
//String的逆序排列
}
});