sorted()方法用来排序。
代码如下:
Stream<String> stream = Stream.of("lvbu","zhangfei", "guanyu", "caocao");
//collect 是终端操作,会消耗掉Stream, 所以stream不能重复使用了,注释掉
// List<String> collect = stream.sorted().collect(Collectors.toList()); // 默认排序 ,根据a-z
// List<String> collect2= stream.sorted(Comparator.reverseOrder()).collect(Collectors.toList()); // 反向排序 z-z
List<String> collect3= stream.sorted(Comparator.comparing(s->s.length())).collect(Collectors.toList()); // 根据名字长短排序 吕布最吃亏
System.out.println(collect3);