概念
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)。
1. 创建
- Arrays.stream()
- Collection.stream()
- Stream.of()
- Stream.iterate()
- Stream.generate()
@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<Integer> integerStream = Stream.of(1, 2, 3, 4);
Stream.iterate(0 ,t ->t+1);
Stream.generate(UUID::randomUUID).limit(10).forEach(System.out::println);
}
2. 中间操作
筛选与切片
方法 | 含义 |
---|
Stream filter(Predicate<? super T> predicate); | 过滤 | Stream limit(long maxSize); | 限制最大记录 | Stream skip(long n); | 跳过n条记录 | Stream distinct(); | 去重 |
@Test
public void test01(){
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<Integer> integerStream2 = Stream.of(1, 2, 3, 4);
integerStream2.skip(3).forEach(System.out::print);
System.out.println();
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() {
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);
}
3.终止操作
匹配、查找
方法 | 含义 |
---|
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() {
Stream<Integer> stream = Stream.of(1, 2, 3, 4,2,1,1);
System.out.println(stream.allMatch(a -> a < 3));
System.out.println();
Stream<Integer> stream1 = Stream.of(1, 2, 3, 4,2,1,1);
System.out.println(stream1.anyMatch(a -> a < 3));
System.out.println();
Stream<Integer> stream2 = Stream.of(1, 2, 3, 4,2,1,1);
System.out.println(stream2.noneMatch(a -> a < 2));
System.out.println();
Stream<Integer> stream3 = Stream.of(1, 2, 3, 4,2,1,1);
System.out.println(stream3.findFirst());
System.out.println();
Stream<Integer> stream4 = Stream.of(1, 2, 3, 4,2,1,1);
System.out.println(stream4.findAny());
System.out.println();
Stream<Integer> stream5 = Stream.of(1, 2, 3, 4,2,1,1);
System.out.println(stream5.count());
System.out.println();
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();
Stream<Integer> stream7 = Stream.of(1, 2, 3, 4,2,1,1);
System.out.println(stream7.max(Integer::compareTo));
System.out.println();
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() {
Stream<Integer> stream = Stream.of(1, 2, 3, 4, 2, 1, 1);
System.out.println(stream.reduce(0,Integer::sum));
System.out.println();
Stream<Integer> stream1 = Stream.of(1, 2, 3, 4, 2, 1, 1);
System.out.println(stream1.reduce(Integer::sum));
System.out.println();
}
收集
方法 | 含义 |
---|
<R, A> R collect(Collector<? super T, A, R> collector); | 将流转为其他形式,Collector的具体实现类决定收集目标 |
@Test
public void test4() {
Stream<Integer> stream = Stream.of(1, 2, 3, 4, 2, 1, 1);
System.out.println(stream.collect(Collectors.toList()));
System.out.println();
Stream<Integer> stream1 = Stream.of(1, 2, 3, 4, 2, 1, 1);
System.out.println(stream1.collect(Collectors.toSet()));
System.out.println();
}
当然可以收集平均值、个数、或新创建结果集以及分组等。
|