一、排序
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);