::语法附近是关键点,基本复制过去就能用,把::改下就好
去重根据对象的某个属性
方法一
List<Integer> collect = list.stream().map(User::getId).distinct().collect(Collectors.toList());
方法二
List<SubjectiveDto> collect = subjectiveDtos.stream().collect(Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(
Comparator.comparing(
SubjectiveDto::getClassifyName))), ArrayList::new));
明显第一种看着舒服点
根据对象的某个属性分组map
方式一
Map<String, List<TopicDO>> questionTypeNameMap = childrenList.stream().collect(Collectors.groupingBy(TopicDO::getQuestionTypeName))
方式二
这个可以求每个分组的个数
Map<ObjectId, Long>distinctTopic=randomEleList.stream().collect(Collectors.groupingBy(TopicDO::getTopicFatherId, Collectors.counting()));
方式三
字段对应整个对象
Map<Long, User> map = userInfoList.stream().collect(Collectors.toMap(User::getUserId, user -> user));
方式4
逗号分隔List
labels.stream().map(MediaLabel::getName).collect(Collectors.joining(","));