前言:流写法也是Java8的新特性,通常跟lambda表达式一起使用,效果非常好,可以参考上一篇文章对lambda表达式的常用场景学习。Java8新特性-Lambda表达式_junewgl的博客-CSDN博客
1.导包
使用Stream方法,通常导包为import java.util.stream.Stream;(可以使用import java.util.stream.*;代替)
2.常用的方法举例
2.1过滤filter()--常用
假如需要对list中的数据过滤,保留字符串不等于“abc”并且不能是空字符串,返回结果是Stream类型。
核心代码:
List<String>list = Arrays.asList("abc", "def", "gkh", "abc", "");
Stream<String>stringStream = list.stream().filter(s -> !s.equals("abc") && !s.isEmpty());
stringStream.forEach(s -> System.out.println(s));
测试结果
如果要把返回的Strem类型改为List类型,使用collect(Collectors.toList());(需要导入import java.util.stream.Collectors;)
List<String>stringStream = list.stream().filter(s -> !s.equals("abc") && !s.isEmpty()).collect(Collectors.toList());
2.2计数count()
// 计算流的个数
long count = stringStream.count();
System.out.println(count);
2.3Stream转化为数组toArray()
List<String>list = Arrays.asList("abc", "def", "gkh", "abc", "");
String[]stringArrays = stringStream.toArray(String[] :: new);
for(String s : stringArrays)System.out.println(s);
2.4Stream转化为列表collect(Collectors.toList())
List<String>list = Arrays.asList("abc", "def", "gkh", "abc", "");
List<String>stringStream = list.stream().filter(s -> !s.equals("abc") && !s.isEmpty()).collect(Collectors.toList());
2.5Stream转化为Setcollect(Collectors.toSet())
List<String>list = Arrays.asList("abc", "def", "gkh", "abc", "");
Stream<String>stringStream = list.stream().filter(s -> !s.equals("abc") && !s.isEmpty());
Set<String>set = stringStream.collect(Collectors.toSet());
set.forEach(System.out::println);
2.6限制输出一个:limit(1)
List<String>list = Arrays.asList("abc", "def", "gkh", "abc", "");
Stream<String>stringStream = list.stream().filter(s -> !s.equals("abc") && !s.isEmpty());
Stream<String>streams = stringStream.limit(1);
streams.forEach(System.out::println);
2.7排序sorted()--常用
List<String>list = Arrays.asList("abc", "def", "gkh", "abc", "","dee");
Stream<String>stringStream = list.stream().filter(s -> !s.equals("abc") && !s.isEmpty());
Stream<String>streams = stringStream.sorted();
streams.forEach(System.out::println);
2.8去重distinct()--常用
List<String>list = Arrays.asList("abc", "def", "gkh", "abc", "","dee");
Stream<String>stringStream = list.stream().filter(s -> !s.isEmpty());
Stream<String>streams = stringStream.distinct();
streams.forEach(System.out::println);
2.9合并多个流方法,比如去重和排序
List<String>list = Arrays.asList("abc", "def", "gkh", "abc", "","dee");
Stream<String>stringStream = list.stream().filter(s -> !s.isEmpty());
Stream<String>streams = stringStream.sorted().distinct();
streams.forEach(System.out::println);
注意对比这里的结果和2.8结果的区别。
3.注意-一个流只能操作一次
参考JavaGuid相关内容,很强!!!
//注意 一个Stream只能操作一次,不能断开,否则会报错。
Stream stream = strings.stream();
//第一次使用
stream.limit(2);
//第二次使用
stream.forEach(System.out::println);
第二次使用会报错
Exception in thread "main" java.lang.IllegalStateException: stream has already been operated upon or closed
4.附上完整测试代码
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/*
test for java
*/
public class Main{
public static void main(String[]args){
// Stream学习
List<String>list = Arrays.asList("abc", "def", "gkh", "abc", "","dee");
Stream<String>stringStream = list.stream().filter(s -> !s.isEmpty());
Stream<String>streams = stringStream.distinct();
streams.forEach(System.out::println);
// Set<String>set = stringStream.collect(Collectors.toSet());
// set.forEach(System.out::println);
// List<String>list = Arrays.asList("abc", "def", "gkh", "abc", "");
// List<String>stringStream = list.stream().filter(s -> !s.equals("abc") && !s.isEmpty()).collect(Collectors.toList());
// stringStream.forEach(s -> System.out.println(s));
// List<String>list = Arrays.asList("abc", "def", "gkh", "abc", "");
// String[]stringArrays = stringStream.toArray(String[] :: new);
// for(String s : stringArrays)System.out.println(s);
// Stream<String>stringStream = list.stream().filter(s -> !s.equals("abc") && !s.isEmpty());
// 计算流的个数
// long count = stringStream.count();
// System.out.println(count);
// String[]stringArrays = stringStream.toArray(String[] :: new);
// for(String s : stringArrays)System.out.println(s);
// stringArrays.forEach(s -> System.out.println(s));
}
}