话不多说 直接上代码
List<String> list = new ArrayList<>();
Collections.addAll(list, "zhangsan", "good", "hello", "jj", "xx", "jj", "jj","balalala","xx", "good");
//1)通过List集合获得Stream流
Stream<String> stream = list.stream();
//2流的中间操作
//2.1 流的筛选与切片
//distinct()去掉重复的数据
stream.distinct().forEach(System.out::println);
System.out.println("-----------------------");
//filter()过滤
// stream.filter(x->x.length() > 4).forEach(System.out::println); //java.lang.IllegalStateException: 流已关闭, 流只能使用一次
list.stream().filter(x->x.length() > 4).forEach(System.out::println);
System.out.println("-----------------------");
//sort()排序
list.stream().sorted(String::compareTo).forEach(System.out::println);
System.out.println("-----------------------");