m放在第二个参数Function函数的参数里面,将结果设置为t。对于toCollection是一个通用的方法,满足treeSet收集集合,再传入需要根据某个属性进行比较的比较器,就能达到去重的效果。
2.限制长度(limit)
List<Integer> list = Arrays.asList(1, 2, 7, 3, 2, 2, 3, 4, 5, 2, 5, 6, 7, 8, 9, 0, 12);
List<Integer> collect1 = list
.stream()
.distinct()
.sorted(Comparator.comparing(Integer::intValue))
.limit(6)
.collect(Collectors.toList());
collect1.forEach(x -> System.out.print(x + " "));
3.跳过(skip)
// 跳过前几项
List<Integer> list = Arrays.asList(1, 2, 7, 3, 2, 2, 3, 4, 5, 2, 5, 6, 7, 8, 9, 0, 12);
List<Integer> collect2 = list
.stream()
.distinct()
.sorted(Comparator.comparing(Integer::intValue))
.skip(3)
.limit(6)
.collect(Collectors.toList());
collect2.forEach(x -> System.out.print(x + " "));