list去重,list转map

对一个list按userId去重

List<String> userIdList = userIdList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getUserId()))), ArrayList::new));

将一个list按userId为key,value为list<A>转成一个map

 Map<String, List<A>> partsMap = resultList.stream().collect(Collectors.toMap(A::getUserId, A ->
              Lists.newArrayList(A), (List<A> newValueList, List<A> oldValueList) ->
        {
           oldValueList.addAll(newValueList);
           return oldValueList;
        }));

将一个list按照年龄大小做去重处理

List<Student> distinctList = studentList.stream() .collect(Collectors.toMap(Student::getId, student -> student, (s1, s2) -> Integer.parseInt(s1.getAge()) < Integer.parseInt(s2.getAge()) ? s1 : s2)) .values() .stream() .collect(Collectors.toList());
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容