
Stream是数据渠道,用于操作数据源(集合、数组等数据)所生成的数据流。
Stream本身不储存元素 Stream不改变源对象,返回操作结果生成的新Stream Stream操作在需要结果时才真正触发执行,也就是说Stream是延迟执行的
Stream和Collection集合区别:Collection是一种静态的内存数据结构,Stream面向CPU,与计算相关的。
1. 创建Stream 从一个数据源获取,如集合、数组。 2. 中间操作 对Stream链式操作,以对数据进行处理。 3. 终止操作 执行终止操作即开始执行中间的操作连,并返回结果,一旦执行终止操作,该Stream不可再被使用(java.lang.IllegalStateException: stream has already been operated upon or closed)。
@Test
public void getStream(){
String[] data = {"a","b"};
// 获取一个串行(顺序)流
Stream<String> stream = Arrays.stream(data);
List<String> data1 = Arrays.asList(data);
// 获取一个并行流
Stream<String> stringStream = data1.parallelStream();
// 串行流转并行流
Stream<String> parallel = stream.parallel();
// 通过Stream.of 获取stream,实际执行的是:Arrays.stream(values);
Stream<Integer> integerStream = Stream.of(1, 2, 3, 4);
// 无限流 迭代
Stream.iterate(0 ,t ->t+1);
// 无限流 生成
Stream.generate(UUID::randomUUID).limit(10).forEach(System.out::println);
}方法 | 含义 |
|---|---|
Stream filter(Predicate<? super T> predicate); | 过滤 |
Stream limit(long maxSize); | 限制最大记录 |
Stream skip(long n); | 跳过n条记录 |
Stream distinct(); | 去重 |
@Test
public void test01(){
// 过滤 filter(Predicate<? super T> predicate)
Stream<Integer> integerStream = Stream.of(1, 2, 3, 4);
integerStream.filter(t -> t%2 ==0 ).forEach(System.out::print);
System.out.println();
// 截断
Stream<Integer> integerStream1 = Stream.of(1, 2, 3, 4);
integerStream1.limit(3).forEach(System.out::print);
System.out.println();
// 跳过 如果元素不足跳过个数,返回空stream
Stream<Integer> integerStream2 = Stream.of(1, 2, 3, 4);
integerStream2.skip(3).forEach(System.out::print);
System.out.println();
// 筛选去重 根据hashcode() 和equals() 对比去重
Stream<Integer> integerStream3 = Stream.of(1, 2, 3, 4,2,1,1);
integerStream3.distinct().forEach(System.out::print);
System.out.println();
}方法 | 含义 |
|---|---|
Stream map(Function<? super T, ? extends R> mapper); | 映射,元素映射成新的元素 |
Stream flatMap(Function<? super T, ? extends Stream<? extends R>> mapper); | 映射,元素转成流,流再进行连接形成新的流 |
IntStream mapToInt(ToIntFunction<? super T> mapper); | 映射,映射成新的元素,返回IntStream |
LongStream mapToLong(ToLongFunction<? super T> mapper); | 映射,映射成新的元素,返回LongStream |
DoubleStream mapToDouble(ToDoubleFunction<? super T> mapper); | 映射,映射成新的元素,返回DoubleStream |
IntStream flatMapToInt(Function<? super T, ? extends IntStream> mapper); | 元素映射成流,返回IntStream |
LongStream flatMapToLong(Function<? super T, ? extends LongStream> mapper); | 元素映射成流,返回LongStream |
DoubleStream flatMapToDouble(Function<? super T, ? extends DoubleStream> mapper); | 元素映射成流,返回DoubleStream |
@Test
public void test02(){
// 映射 将元素准换成其他形式或提取信息,参数为函数,函数将会被应用到每一个元素,并映射成新的元素
Stream<Integer> stream = Stream.of(1, 2, 3, 4,2,1,1);
stream.map(val -> val * 10).forEach(System.out::print);
System.out.println();
List<DemoUser> demoUsers = Arrays.asList(new DemoUser("a", 10), new DemoUser("b", 20));
Stream<String> stringStream = demoUsers.stream().map(DemoUser::getName);
stringStream.forEach(System.out::print);
System.out.println();
// 映射 将元素准换成另一个流,参数为函数,函数将会被应用到每一个元素,并映连接成新的流
Stream<Integer> stream1 = Stream.of(1, 2, 3, 4,2,1,1);
Stream<Double> doubleStream = stream1.flatMap((val) -> Stream.of(val * 1.0));
doubleStream.forEach(System.out::print);
System.out.println();
}
@Data
@AllArgsConstructor
class DemoUser{
private String name;
private Integer age;
}方法 | 含义 |
|---|---|
sorted() | 产生新的流,按自然顺序排序,需要实现 Comparator 接口 |
sorted(Comparator com) | 产生新的流,按比较器顺序排序 |
@Test
public void test() {
// 自然排序,需要实现Comparable接口
Stream<Integer> stream = Stream.of(1, 2, 3, 4,2,1,1);
stream.sorted().forEach(System.out::print);
System.out.println();
// 比较器排序
List<DemoUser> demoUsers = Arrays.asList(new DemoUser("a", 10), new DemoUser("b", 20));
Stream<DemoUser> stringStream = demoUsers.stream().sorted( (u1, u2) -> u1.getAge().compareTo(u2.getAge()));
stringStream.forEach(System.out::print);
}方法 | 含义 |
|---|---|
boolean allMatch(Predicate<? super T> predicate); | 检查是否全匹配 |
boolean anyMatch(Predicate<? super T> predicate); | 至少匹配一个 |
boolean noneMatch(Predicate<? super T> predicate); | 一个也没有匹配上 |
Optional findFirst(); | 获取第一个 |
Optional findAny(); | 获取任意一个 |
long count(); | 计算总个数 |
Optional min(Comparator<? super T> comparator); | 返回最小的 |
Optional max(Comparator<? super T> comparator); | 返回最大的 |
void forEach(Consumer<? super T> action); | 内部迭代 |
@Test
public void test2() {
// 匹配 allMatch 是否都满足条件
Stream<Integer> stream = Stream.of(1, 2, 3, 4,2,1,1);
System.out.println(stream.allMatch(a -> a < 3));
System.out.println();
// 匹配 anyMatch 至少匹配一个元素
Stream<Integer> stream1 = Stream.of(1, 2, 3, 4,2,1,1);
System.out.println(stream1.anyMatch(a -> a < 3));
System.out.println();
// 匹配 noneMatch 一个也没有匹配上
Stream<Integer> stream2 = Stream.of(1, 2, 3, 4,2,1,1);
System.out.println(stream2.noneMatch(a -> a < 2));
System.out.println();
// 匹配 findFirst 返回第一个 Optional<T>
Stream<Integer> stream3 = Stream.of(1, 2, 3, 4,2,1,1);
System.out.println(stream3.findFirst());
System.out.println();
// 匹配 findAny 返回任意一个 Optional<T>
Stream<Integer> stream4 = Stream.of(1, 2, 3, 4,2,1,1);
System.out.println(stream4.findAny());
System.out.println();
// 匹配 count 返回总数
Stream<Integer> stream5 = Stream.of(1, 2, 3, 4,2,1,1);
System.out.println(stream5.count());
System.out.println();
// 匹配 min 返回最小值
Stream<Integer> stream6 = Stream.of(1, 2, 3, 4,2,1,1);
System.out.println(stream6.min((a,b) -> a.compareTo(b)));
System.out.println();
// 匹配 max 返回最大值
Stream<Integer> stream7 = Stream.of(1, 2, 3, 4,2,1,1);
System.out.println(stream7.max(Integer::compareTo));
System.out.println();
// 匹配 forEach 内部迭代
Stream<Integer> stream8 = Stream.of(1, 2, 3, 4,2,1,1);
stream8.forEach(System.out::println);
System.out.println();
}方法 | 含义 |
|---|---|
T reduce(T identity, BinaryOperator accumulator); | 将元素反复结合,返回一个新的值 |
Optional reduce(BinaryOperator accumulator); | 将元素反复结合,返回Optional |
@Test
public void test3() {
// reduce 反复结合 返回 Object
Stream<Integer> stream = Stream.of(1, 2, 3, 4, 2, 1, 1);
System.out.println(stream.reduce(0,Integer::sum));// 反复累加 14
System.out.println();
// reduce 反复结合 返回 Optional
Stream<Integer> stream1 = Stream.of(1, 2, 3, 4, 2, 1, 1);
System.out.println(stream1.reduce(Integer::sum)); // 返回 Optional[14]
System.out.println();
}方法 | 含义 |
|---|---|
<R, A> R collect(Collector<? super T, A, R> collector); | 将流转为其他形式,Collector的具体实现类决定收集目标 |
@Test
public void test4() {
// 收集到list
Stream<Integer> stream = Stream.of(1, 2, 3, 4, 2, 1, 1);
System.out.println(stream.collect(Collectors.toList())); //[1, 2, 3, 4, 2, 1, 1]
System.out.println();
// 收集到list
Stream<Integer> stream1 = Stream.of(1, 2, 3, 4, 2, 1, 1);
System.out.println(stream1.collect(Collectors.toSet())); //[1, 2, 3, 4]
System.out.println();
}当然可以收集平均值、个数、或新创建结果集以及分组等。