java8 list操作

排序 降序

 // 根据id升序排序
dataList.stream().sorted(Comparator.comparing(EventResponseEditData::getId)).collect(Collectors.toList());
// 降序
dataList.sort((o1, o2) -> o2.getId().compareTo(o1.getId()));

去重

List<SourceTargetDto> treeSetClasData = allTableData.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(
                () -> new TreeSet<>(Comparator.comparing(o -> o.getTargetName() + "#" + o.getDatabaseName() + "#" + o.getTableName() +"#"+ o.getChildId()))),
                ArrayList::new));

分组

Map<String, List<UmpRedisEpEvent>> map = epEvents.stream().collect(Collectors.groupingBy(UmpRedisEpEvent::getHostIdPathRu));

取list中的对象的属性最大值

 epEventsN = map.values().stream().map(events ->
                events.stream().max(Comparator.comparing(UmpRedisEpEvent::getEventTime))).
                filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList());

转换list类型

 List<AccountAuthDeptVo> deptVoList = deptMapper.selectListVO(null);
 List<OrgTerFileDataVO> epFileList = deptVoList.stream().map(deptVo -> {
            OrgTerFileDataVO fileDataVO = new OrgTerFileDataVO();
            BeanUtils.copyProperties(deptVo, fileDataVO);
            return fileDataVO;
        }).collect(Collectors.toList());

比较两个list取差集

List<TransferWarehouseO2ODetailDto> newTransfer = transferWarehouseO2ODetailDtoList.stream().filter(m -> !wlTransferWarehouseDList.stream().map(d -> d.getProviderPackId()).collect(Collectors.toList()).contains(m.getProviderPackId())).collect(Collectors.toList());

取两个集合交集

List<String> intersection = list1.stream().filter(item -> list2.contains(item)).collect(toList());

取两个集合差集

基础数据类型
List<String> reduceList = list1.stream().filter(item -> !list2.contains(item)).collect(toList());
自定义数据类型
List<FileLocal> intersection = fileLocalOne.stream().filter(userInfo ->
                fileLocalTwo.stream().map(FileLocal::getId).collect(Collectors.toList()).contains(userInfo.getId())
        ).collect(Collectors.toList());

两个集合去重并集

List<String> list =new ArrayList<>(list1);
    list.addAll(list2);
    List<String> listAllDistinct = list.stream().distinct().collect(toList());
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值