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