Java8常用的List和Map转化方法

 一、排序

java<Bean>


a. 单属性

// 正序
List<Person> collect = personList.stream().sorted(Comparator.comparing(Person::getUploadTime)).collect(Collectors.toList());

// 倒序
List<Person> collect = personList.stream().sorted((p1, p2) -> p2.getUploadTime().compareTo(p1.getUploadTime())).collect(Collectors.toList());

b. 多属性 

//排序 
    resultList = resultList.stream()
            .sorted(
                    Comparator.comparing(ProjectEfficacyVO::getProjectStatus).reversed()
                            .thenComparing(ProjectEfficacyVO::getEfficacyTotal)
                            .thenComparing(ProjectEfficacyVO::getDeptId)
                            .thenComparing(ProjectEfficacyVO::getProjectManageName)
            ).collect(Collectors.toList());

注意:排序的方式默认为“升序”,如果需要根据字段进行“降序”,则需要加入reversed()

List<Map>


1.  升序排序

  // 按身高升序
        List<Map<String, Object>> sortedByHeightAscList =
                studentList.stream().sorted(Comparator.comparing(h -> ((BigDecimal) h.get("height"))))
                        .collect(Collectors.toList());

2.  降序排序

  // 按身高降序
        List<Map<String, Object>> sortedByHeightDescList = 
                studentList.stream().sorted((h1, h2) -> ((BigDecimal)h2.get("height")).compareTo((BigDecimal)h1.get("height")))
                        .collect(Collectors.toList());

二、转化

1. List<T> 将某一个属性转化为List<String>

List<String> ids = list.stream().map(Sample2ProjectExperimentInfoEntity::getSampleType).collect(Collectors.toList());

2.将map的key转化为List<String>

List<String> currentUploadPatienIdList  = sampleDatasConvertMap.keySet()
                .stream()
                .collect(Collectors.toList());

3.根据属性过滤

List<PdfCellDto> result = currentRowCellList.stream().
                    filter(PdfCellDto -> PdfCellDto.getText().equals("变更类别")).collect(Collectors.toList());

3.1 根据单个属性去重
userList = userList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(User :: getUserid))), ArrayList::new));

3.2 List<T>根据多个属性去重
List<SysUserEntity> distinctClass = userList.stream().
                collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o ->
                o.getName() + ";" + o.getPassword()))), ArrayList::new));

4.List<T>转化为Map<String,String> key:属性A,value:属性B

Map<String, String> outFlowNodesMap =
                        outFlowNodesList.stream().collect(Collectors.toMap(Sample2ProjectWorkflowNodeEntity::getNodeId, Sample2ProjectWorkflowNodeEntity::getNodeType));

5.List<T>转化为Map<String,String> key:属性A+属性B+......,value:属性N

Map<String,String> checkErrorInfoMap = checkErrorInfoList.stream().
                collect(Collectors.toMap(k -> k.getBarcode()+"@"+k.getExcelColumn(), k -> k.getDescription()));

6.List<T>转化为Map<String,T>

Map<String, User> map = users.stream().collect(Collectors.toMap(User::getName, Function.identity()));

7.单个字段分分组

//根据年龄分组
List<User> userList = new ArrayList<>();
//方法引用实现
Map<Integer, List<User>> ageGroupMap = userList.stream().collect(Collectors.groupingBy(User::getAge));
 
//lambda表达式实现
Map<Integer, List<User>> ageGroupMap2 = userList.stream().collect(Collectors.groupingBy(x->x.getAge())); 

-- List<Map>根据某个字段进行分组

Map<Object, List<Map<String, Object>>> groupedMap = 
    mapList.stream().collect(Collectors.groupingBy(map -> map.get("city")));
 

8.多个标签分组

//根据年龄分组
List<User> userList = new ArrayList<>();
//方法引用实现
Map<Integer, List<User>> ageGroupMap = userList.stream().collect(Collectors.groupingBy(User::getAge));
 
//lambda表达式实现
Map<Integer, List<User>> ageGroupMap2 = userList.stream().collect(Collectors.groupingBy(x->x.getAge())); 

 9. List<Entity>转化为List<DTO>

List<LabelApiDTO> returnLabelApiDtoList = Lists.transform(labelList, (lableEntity)->{
            LabelApiDTO dto = new LabelApiDTO();
            // 属性copy
            BeanUtils.copyProperties(lableEntity, dto);
            dto.setLabelType(LabelDataTypeEnum.getName(lableEntity.getDataType()));
            dto.setTabelName(entity.getEntityLabelTableName());
            dto.setTarField(lableEntity.getClickhouseField());
            dto.setTarFieldType(LabelDataTypeEnum.getName(lableEntity.getDataType()));
            return dto;
        });

List均分

List<List<String>> partitions = Lists.partition(fundCodeList,800);

10、根据对象属性查找

boolean check = allContractList.stream().anyMatch(obj -> entity.getContractNo().equals(obj.getContractNo()));

三、聚合函数

1. reduce进行累加

BigDecimal totalDutyDays = list.stream().map(ProjectCostEntity::getPreWorkAmount).reduce(BigDecimal.ZERO, BigDecimal::add);

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值