- 前言
- 一、Stream创建及注意事项
- 二、Stream常用处理方法
- 三、并行Stream流创建及使用
- 结尾
前言
注意:Stream和IO流(InputStream/OutputStream)没有任何关系,请暂时忘记对传统IO流的固有印象!
Stream流式思想类似于工厂车间的“生产流水线”,Stream流不是一种数据结构,不保存数据,而是对数据进行加工
处理。Stream可以看作是流水线上的一个工序。在流水线上,通过多个工序让一个原材料加工成一个商品。
Stream API能让我们快速完成许多复杂的操作,如筛选、切片、映射、查找、去除重复,统计,匹配和归约。
一、Stream创建及注意事项
- Stream只能操作一次
- Stream方法返回的是新的流
- Stream不调用终结方法,中间的操作不会执行
package stream;
import java.util.*;
import java.util.stream.Stream;
public class StreamGetAndNotice {
public void testGet() {
// 方式1 : 根据Collection获取流
// Collection接口中有一个默认的方法: default Stream stream()
List list = new ArrayList<>();
Stream stream1 = list.stream();
Set set = new HashSet<>();
Stream stream2 = set.stream();
Map map = new HashMap<>();
Stream stream3 = map.keySet().stream();
Stream stream4 = map.values().stream();
Stream> stream5 = map.entrySet().stream();
// 方式2 : Stream中的静态方法of获取流
// static Stream of(T... values)
Stream stream6 = Stream.of("aa", "bb", "cc");
String[] strs = {"aa", "bb", "cc"};
Stream stream7 = Stream.of(strs);
// 基本数据类型的数组行不行?不行的,会将整个数组看做一个元素进行操作.
int[] arr = {11, 22, 33};
Stream stream8 = Stream.of(arr);
}
public void notice() {
Stream stream = Stream.of("aa", "bb", "cc");
// 1. Stream只能操作一次
// long count = stream.count();
// long count2 = stream.count();
// 2. Stream方法返回的是新的流
// Stream limit = stream.limit(1);
// System.out.println("stream" + stream);
// System.out.println("limit" + limit);
// 3. Stream不调用终结方法,中间的操作不会执行
stream.filter((s) -> {
System.out.println(s);
return true;
}).count();
}
}
二、Stream常用处理方法
package stream;
import stream.entity.Person;
import java.util.Optional;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class StreamCommonFunc {
Stream stream = Stream.of("1111", "222", "333", "44", "55", "55");
public void testForeach() {
stream.forEach(System.out::println);
}
public void testCount() {
System.out.println(stream.count());
}
public void testFilter() {
stream.filter(s -> s.length() == 3).forEach(System.out::println);
}
public void testLimit() {
stream.limit(3).forEach(System.out::println);
}
public void testSkip() {
stream.skip(3).forEach(System.out::println);
}
public void testMap() {
Stream s = stream.map(Integer::parseInt);
s.forEach(System.out::println);
}
public void testSorted() {
Stream s = stream.map(Integer::parseInt);
s.sorted((i1, i2) -> i2 - i1).forEach(System.out::println);
}
public void testDistinct() {
stream.distinct().forEach(System.out::println);
}
public void testMatch() {
System.out.println(stream.allMatch(s -> s.length() > 2));
System.out.println(stream.anyMatch(s -> s.length() > 2));
System.out.println(stream.noneMatch(s -> s.length() > 2));
}
public void testFind() {
System.out.println(stream.findFirst().get());
}
public void testMax_Min() {
// 获取最大值
// 1, 3, 5, 6
Optional max = Stream.of(5, 3, 6, 1).max((o1, o2) -> o1 - o2);
System.out.println("最大值: " + max.get());
// 获取最小值
// 1, 3, 5, 6
Optional min = Stream.of(5, 3, 6, 1).min((o1, o2) -> o1 - o2);
System.out.println("最小值: " + min.get());
}
public void testReduce() {
// T reduce(T identity, BinaryOperator accumulator);
// T identity: 默认值
// BinaryOperator accumulator: 对数据进行处理的方式
// reduce如何执行?
// 第一次, 将默认值赋值给x, 取出集合第一元素赋值给y
// 第二次, 将上一次返回的结果赋值x, 取出集合第二元素赋值给y
// 第三次, 将上一次返回的结果赋值x, 取出集合第三元素赋值给y
// 第四次, 将上一次返回的结果赋值x, 取出集合第四元素赋值给y
int reduce = Stream.of(4, 5, 3, 9).reduce(0, (x, y) -> {
System.out.println("x = " + x + ", y = " + y);
return x + y;
});
System.out.println("reduce = " + reduce); // 21
// 获取最大值
Integer max = Stream.of(4, 5, 3, 9).reduce(0, (x, y) -> {
return x > y ? x : y;
});
System.out.println("max = " + max);
}
public void testMapReduce() {
// 求出所有年龄的总和
// 1.得到所有的年龄
// 2.让年龄相加
Integer totalAge = Stream.of(
new Person("刘德华", 58),
new Person("张学友", 56),
new Person("郭富城", 54),
new Person("黎明", 52))
.map((p) -> p.getAge()).reduce(0, Integer::sum);
System.out.println("totalAge = " + totalAge);
// 找出最大年龄
// 1.得到所有的年龄
// 2.获取最大的年龄
Integer maxAge = Stream.of(
new Person("刘德华", 58),
new Person("张学友", 56),
new Person("郭富城", 54),
new Person("黎明", 52))
.map(p -> p.getAge())
.reduce(0, Math::max);
System.out.println("maxAge = " + maxAge);
// 统计 a 出现的次数
// 1 0 0 1 0 1
Integer count = Stream.of("a", "c", "b", "a", "b", "a")
.map(s -> {
if (s == "a") {
return 1;
} else {
return 0;
}
})
.reduce(0, Integer::sum);
System.out.println("count = " + count);
}
public void testNumericStream() {
// Integer占用的内存比int多,在Stream流操作中会自动装箱和拆箱
Stream stream = Stream.of(1, 2, 3, 4, 5);
// 把大于3的打印出来
// stream.filter(n -> n > 3).forEach(System.out::println);
// IntStream mapToInt(ToIntFunction super T> mapper);
// IntStream: 内部操作的是int类型的数据,就可以节省内存,减少自动装箱和拆箱
IntStream intStream = Stream.of(1, 2, 3, 4, 5).mapToInt(Integer::intValue);
intStream.filter(n -> n > 3).forEach(System.out::println);
}
public void testContact() {
Stream streamA = Stream.of("张三");
Stream streamB = Stream.of("李四");
// 合并成一个流
Stream newStream = Stream.concat(streamA, streamB);
// 注意:合并流之后,不能操作之前的流啦.
// streamA.forEach(System.out::println);
newStream.forEach(System.out::println);
}
}
三、并行Stream流创建及使用
package com.cloud.jdk8.stream;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class StreamParallel {
@Test
public void testGetParallelStream() {
// 掌握获取并行Stream流的两种方式
// 方式一:直接获取并行的Stream流
List list = new ArrayList<>();
Stream stream = list.parallelStream();
// 方式二:将串行流转成并行流
Stream parallel = list.stream().parallel();
}
@Test
public void testParallel() {
Stream.of(4, 5, 3, 9, 1, 2, 6)
.parallel() // 转成并行流
.filter(s -> {
System.out.println(Thread.currentThread() + "::" + s);
return s > 3;
})
.count();
}
@Test
public void parallelStreamNotice() {
ArrayList list = new ArrayList<>();
// 解决parallelStream线程安全问题方案一: 使用同步代码块
// 解决parallelStream线程安全问题方案二: 使用线程安全的集合
// Vector v = new Vector();
// 解决parallelStream线程安全问题方案三: 调用Stream流的collect/toArray
List collect = IntStream.rangeClosed(1, 1000)
.parallel()
.boxed()
.collect(Collectors.toList());
System.out.println("collect.size = " + collect.size());
}
}
结尾
- 感谢大家的耐心阅读,如有建议请私信或评论留言。
- 如有收获,劳烦支持,关注、点赞、评论、收藏均可,博主会经常更新,与大家共同进步



