| 操作 | 状态 | 方法 |
|---|---|---|
| 中间操作 | 无状态 | unorder() |
| filter() | ||
| map() | ||
| mapToInt() | ||
| mapToDouble() | ||
| mapToLong() | ||
| flatMap() | ||
| flatMapToInt() | ||
| flatMapToDouble | ||
| flatMapToLong | ||
| peek() | ||
| 有状态 | distinct() | |
| sorted() | ||
| limit() | ||
| skip() | ||
| 结束操作 | 非短路操作 | forEach() |
| forEarchOrdered() | ||
| toArray() | ||
| reduce() | ||
| collect() | ||
| max() | ||
| min() | ||
| count() | ||
| 短路操作 | anyMatch() | |
| allMatch() | ||
| noneMatch() | ||
| findFirst() | ||
| findAny() |
- 无状态:指元素不受之前元素的影响
- 有状态:指该操作只有拿到所有元素后才能继续下去
- 非短路操作:指不许处理所有元素才能得到最终结果
- 短路操作:指遇到某些符合条件的元素就可以得到最终结果,如:a||b 只有a为true,则无需执行判断b的结果
- 集合Collection下的 stream() 和 parallelStream()方法:
Listlist = new ArrayList<>(); Stream stream = list.stream(); //获取一个顺序流 Stream parallelStream = list.parallelStream(); //获取一个并行流
- 数组下获取stream()
Integer[] nums = new Integer[10]; Streamstream = Arrays.stream(nums);
- Stream中的静态方法
Streamstream = Stream.of(1,2,3,4,5,6); Stream stream2 = Stream.iterate(0, (x) -> x + 2).limit(6); Stream stream3 = Stream.generate(Math::random).limit(2);
- BufferedReader.lines() 方法获取stream
BufferedReader reader = new BufferedReader(new FileReader("F:\test_stream.txt"));
Stream lineStream = reader.lines();
- Pattern.splitAsStream()方法获取Stream
Pattern pattern = Pattern.compile(",");
Stream stringStream = pattern.splitAsStream("a,b,c,d");
流的中间操作
筛选与切片
- filter:过滤流中的某些元素
- limit(n):获取n个元素
- skip(n):跳过n个元素,配合limit(n)可实现分页
- distinct:通过流中的元素hashCode()和equals()去重元素
Stream映射stream = Stream.of(6, 4, 6, 7, 3, 9, 8, 10, 12, 14, 14); Stream newStream = stream.filter(s -> s > 5) //6 6 7 9 8 10 12 14 14 .distinct() //6 7 9 8 10 12 14 .skip(2) //9 8 10 12 14 .limit(2); //9 8 newStream.forEach(System.out::println);
- map:接受一个函数作为参数,该函数会被应用到每个元素上,并将其映射成为一个新的元素
- flatMap:接受一个函数作为参数,将六种的每一个指都换成另一个流,然后把所有流连接成一个流
List排序list = Arrays.asList("a,b,c", "1,2,3"); //将每个元素转成一个新的且不带逗号的元素 Stream s1 = list.stream().map(s -> s.replaceAll(",", "")); s1.forEach(System.out::println); // abc 123 Stream s3 = list.stream().flatMap(s -> { //将每个元素转换成一个stream String[] split = s.split(","); Stream s2 = Arrays.stream(split); return s2; }); s3.forEach(System.out::println); // a b c 1 2 3
- sorted():自然排序,六种元素需要实现Comparable接口
- sorted(Comparator comp):定制排序,自定义Comparator排序器
List消费list = Arrays.asList("aa", "ff", "dd"); //String 类自身已实现Compareable接口 list.stream().sorted().forEach(System.out::println);// aa dd ff Student s1 = new Student("aa", 10); Student s2 = new Student("bb", 20); Student s3 = new Student("aa", 30); Student s4 = new Student("dd", 40); List studentList = Arrays.asList(s1, s2, s3, s4); //自定义排序:先按姓名升序,姓名相同则按年龄升序 studentList.stream().sorted( (o1, o2) -> { if (o1.getName().equals(o2.getName())) { return o1.getAge() - o2.getAge(); } else { return o1.getName().compareTo(o2.getName()); } } ).forEach(System.out::println);
- peek:如同map,能够得到流中的每一个元素。但是map接收的是一个Function表达式,有返回值。但是peek接收的是Consumer表达式,没有返回值
Student s1 = new Student("aa", 10);
Student s2 = new Student("bb", 20);
List studentList = Arrays.asList(s1, s2);
studentList.stream()
.peek(o -> o.setAge(100))
.forEach(System.out::println);
//结果:
Student{name='aa', age=100}
Student{name='bb', age=100}
流终止操作
匹配、聚合操作
- allMatch:接收一个Predicate函数,当流中的每个元素都符合该断言时才返回true,否则返回false
- noneMatch:接收一个Predicate函数,当流中每个元素都不符合断言时才返回true,否则返回false
- anyMatch:接收一个Predicate函数,只有流中的有一个元素满足该断言则返回true,否则返回false
- findFirst:返回流中第一个元素
- findAny:返回流中的任意一个元素
- count:返回流中元素的中个数
- max:返回流中元素最大值
- min:返回流中元素最小值
List约束操作list = Arrays.asList(1, 2, 3, 4, 5); boolean allMatch = list.stream().allMatch(e -> e > 10); //false boolean noneMatch = list.stream().noneMatch(e -> e > 10); //true boolean anyMatch = list.stream().anyMatch(e -> e > 4); //true Integer findFirst = list.stream().findFirst().get(); //1 Integer findAny = list.stream().findAny().get(); //1 long count = list.stream().count(); //5 Integer max = list.stream().max(Integer::compareTo).get(); //5 Integer min = list.stream().min(Integer::compareTo).get(); //1
- Optional
reduce(BinaryOperator accumulator):第一次执行时,accumulator 函数的第一个参数为流中的第一个元素,第二个参数为流中元素的第二个元素。第二次执行时,第一个参数为第一次函数执行的结果,第二个参数为流中的第三个元素;依次类推。 - T reduce(T identity, BinaryOperator
accumulator):流程跟上面一样,只是第一次执行时,accumulator 函数的第一个参数为 identity,而第二个参数为流中的第一个元素。 - U reduce(U identity,BiFunction accumulator,BinaryOperator combiner):在串行流(stream)中,该方法跟第二个方法一样,即第三个参数 combiner 不会起作用。
在并行流(parallelStream)中,我们知道流被 fork join 出多个线程进行执行,此时每个线程的执行流程就跟第二个方法 reduce(identity,accumulator)一样。
而第三个参数 combiner 函数,则是将每个线程的执行结果当成一个新的流,然后使用第一个方法 reduce(accumulator)流程进行规约。
//经过测试,当元素个数小于24时,并行时线程数等于元素个数,当大于等于24时,并行时线程数为16 List收集操作Collectorlist = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24); Integer v = list.stream().reduce((x1, x2) -> x1 + x2).get(); System.out.println(v); // 300 Integer v1 = list.stream().reduce(10, (x1, x2) -> x1 + x2); System.out.println(v1); //310 Integer v2 = list.stream().reduce(0, (x1, x2) -> { System.out.println("stream accumulator: x1:" + x1 + " x2:" + x2); return x1 - x2; }, (x1, x2) -> { System.out.println("stream combiner: x1:" + x1 + " x2:" + x2); return x1 * x2; }); System.out.println(v2); // -300 Integer v3 = list.parallelStream().reduce(0, (x1, x2) -> { System.out.println("parallelStream accumulator: x1:" + x1 + " x2:" + x2); return x1 - x2; }, (x1, x2) -> { System.out.println("parallelStream combiner: x1:" + x1 + " x2:" + x2); return x1 * x2; }); System.out.println(v3); //197474048
Collector
是一个接口,有以下 5 个抽象方法:
-
Supplier supplier():创建一个结果容器 A
-
BiConsumer accumulator():消费型接口,第一个参数为容器 A,第二个参数为流中元素 T。
-
BinaryOperator combiner():函数接口,该参数的作用跟上一个方法(reduce)中的 combiner 参数一样,将并行流中各个子进程的运行结果(accumulator 函数操作后的容器 A)进行合并。
-
Function finisher():函数式接口,参数为:容器 A,返回类型为:collect 方法最终想要的结果 R。
-
Set
characteristics():返回一个不可变的 Set 集合,用来表明该 Collector 的特征。
有以下三个特征:
CONCURRENT:表示此收集器支持并发。
UNORDERED:表示该收集操作不会保留流中元素原有的顺序。
IDENTITY_FINISH:表示 finisher 参数只是标识而已,可忽略。
Student s1 = new Student("aa", 10,1);
Student s2 = new Student("bb", 20,2);
Student s3 = new Student("cc", 10,3);
List list = Arrays.asList(s1, s2, s3);
//装成list
List ageList = list.stream().map(Student::getAge).collect(Collectors.toList()); // [10, 20, 10]
//转成set
Set ageSet = list.stream().map(Student::getAge).collect(Collectors.toSet()); // [20, 10]
//转成map,注:key不能相同,否则报错
Map studentMap = list.stream().collect(Collectors.toMap(Student::getName, Student::getAge)); // {cc=10, bb=20, aa=10}
//字符串分隔符连接
String joinName = list.stream().map(Student::getName).collect(Collectors.joining(",", "(", ")")); // (aa,bb,cc)
//聚合操作
//1.学生总数
Long count = list.stream().collect(Collectors.counting()); // 3
//2.最大年龄 (最小的minBy同理)
Integer maxAge = list.stream().map(Student::getAge).collect(Collectors.maxBy(Integer::compare)).get(); // 20
//3.所有人的年龄
Integer sumAge = list.stream().collect(Collectors.summingInt(Student::getAge)); // 40
//4.平均年龄
Double averageAge = list.stream().collect(Collectors.averagingDouble(Student::getAge)); // 13.333333333333334
// 带上以上所有方法
DoubleSummaryStatistics statistics = list.stream().collect(Collectors.summarizingDouble(Student::getAge));
System.out.println("count:" + statistics.getCount() + ",max:" + statistics.getMax() + ",sum:" + statistics.getSum() + ",average:" + statistics.getAverage());
//分组
Map> ageMap = list.stream().collect(Collectors.groupingBy(Student::getAge));
//多重分组,先根据类型分再根据年龄分
Map>> typeAgeMap = list.stream().collect(Collectors.groupingBy(Student::getType, Collectors.groupingBy(Student::getAge)));
//分区
//分成两部分,一部分大于10岁,一部分小于等于10岁
Map> partMap = list.stream().collect(Collectors.partitioningBy(v -> v.getAge() > 10));
//规约
Integer allAge = list.stream().map(Student::getAge).collect(Collectors.reducing(Integer::sum)).get(); //40



