List排序,去重

1.根据指定字段进行升序排序

        treeNodes.stream().sorted(Comparator.comparing(SysResource ::getResourceIndex)).collect(Collectors.toList());

    1.1多字段排序

        list = list.sorted(Comparator.comparing(Student::getName).thenComparing(Student::getAge)).collect(Collectors.toList());

2.根据指定字段降序排序

        treeNodes.stream().sorted(Comparator.comparing(SysResource ::getResourceIndex).reversed()).collect(Collectors.toList());   

3.根据指定字段去重

        userResourceByUserId.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() ->new TreeSet<>(Comparator.comparing(SysResource::getId))),ArrayList::new));

4.合并去重(去重的流只能使用一次,再次使用需要重新创建流)

        //取第一个list的前3个做为第一个流

        Stream<String> list1 = list.stream().limit(3);

        //获取除第一个第一个后的作为第二个流

        Stream<String> list2 = list.stream().skip(1);

        //合并1 2流

        Stream.concat(stream1,stream2).forEach(System.out::println);

        System.out.println("-----");

        //合并1 2流 并且去除重复元素

        Stream.concat(stream1,stream2).distinct().forEach(s -> System.out.println(s));

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容