Stream流的使用步骤:
先得到一条Stream流(流水线),并把数据放上去
获取方式 | 方法名 | 说明 |
单列集合 | default Stream<E> stream() | Collection中的默认方法 |
双列集合 | 无 | 无法直接使用stream流 |
数组 | public static <T> Stream<T> stream(T] array) | Arrays工具类中的静态方法 |
一堆零散数据 | public static<T> Stream<T> of(T... values) | Stream接口中的静态方法 |
使用中间方法对流水线上的数据进行操作
名称 | 说明 |
Stream<T> filter(Predicate<? super T> predicate) | 过滤 |
Stream<T> limit(long maxSize) | 获取前几个元素 |
Stream<T> skip(long n) | 跳过前几个元素 |
Stream<T> distinct() | 元素去重,依赖(hashCode和equals方法) |
static <T> Stream<T> concat(Stream a, Stream b) | 合并a和b两个流为一个流 |
Stream<R> map(Function<T, R> mapper) | 转换流中的数据类型 |
使用终结方法对流水线上的数据进行操作
名称 | 说明 |
void forEach(Consumer action) | 遍历 |
long count() | 统计 |
toArray() | 收集流中的数据,放到数组中 |
collect(Collector collector) | 收集流中的数据,放到集合中 |
小结:
1. Stream流的作用
结合了Lambda表达式,简化集合、数组的操作
2.Stream的使用步聚
获取Stream流对象
使用中间方法处理数据
使用终结方法处理数据
3.如何获取Stream流对象
单列集合:Collection中的默认方法stream
双列集合:不能直接获取
数组:Arrays工具类型中的静态方法stream
一堆零散的数据:Stream接口中的静态方法of
4.常见方法
中间方法:
filter, limit, skip, distinct, concat, map
终结方法:
forEach, count, collect