Java8常用的List和Map转化方法

 一、排序

java


a. 单属性

listDevs.sort(Comparator.comparing(Developer::getAge));

b. 多属性 

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

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

List


1.  升序排序

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

2.  降序排序

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

二、转化

1. List 将某一个属性转化为List

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

2.将map的key转化为List

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

根据属性过滤

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

3.List根据多个属性去重

List distinctClass = userList.stream().
                collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o ->
                o.getName() + ";" + o.getPassword()))), ArrayList::new));

4.List转化为Map key:属性A,value:属性B

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

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

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

6.List转化为Map

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

7.单个字段分分组

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

8.多个标签分组

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

 9. List转化为List

List 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;
        });

你可能感兴趣的:(java基础,java)