java java8 List去重,按指定字段分组,按字段排序操作(持续更新)

//按指定字段去重(java8)
List<hysysLeftDataDTO> countLeftData = hysysLeftDataDTOList.stream().collect(Collectors.collectingAndThen(
	Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(hysysLeftDataDTO::getFirstLeftTitle))),ArrayList::new
));

//按指定字段分组(单字段java8)
Map<String, List<hysysLeftDataDTO>> groupBy = hysysLeftDataDTOList.stream().collect(Collectors.groupingBy(hysysLeftDataDTO::getFirstLeftTitle));
//按指定字段分组(多字段java8)
Map<String, List<LeftDataDTO>> groupBy = leftDataDTOList.stream().collect(
	Collectors.groupingBy(
		leftDataDTO -> leftDataDTO.getFileCode() +'/'+ leftDataDTO.getFileName() +'/'+ leftDataDTO.getDocumentName()

//单字段排序
firstGroupByCodeNameDoc.sort((a,b)->a.getArea().compareTo(b.getArea()));
//多字段排序(非Java8)
Collections.sort(firstGroupByCodeNameDoc, new Comparator<LeftDataDTO>() {
	@Override
	public int compare(LeftDataDTO o1, LeftDataDTO o2) {
		if(o2.getArea().compareTo(o1.getArea()) != 0){
			return o1.getArea().compareTo(o2.getArea());
		}
		else{
			return o1.getDevice().compareTo(o2.getDevice());
		}
	}
	});
));

//循环遍历分组后的值与取值
for (Map.Entry<String, List<hysysLeftDataDTO>> stringListEntry : groupBy.entrySet()) {
            //获取分组的value值
            List<hysysLeftDataDTO> groupByFirstLeftTitle = stringListEntry.getValue();
            CellRange firstTitle = sheet.getCellRange(LeftCount,1);
            firstTitle.setValue(stringListEntry.getKey());

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值