Stream API

Stream 是数据渠道,用于操作数据源(集合、数组等)所生成的元素序列。

注意点:

  1. Stream 自己不会存储元素。
  2. Stream 不会改变原对象。相反,他们会返回一个持有结果的新的 Steam。
  3. Steam 操作是延迟执行的。这意味这他们会等到需要结果的时候在执行。即一旦执行终止操作,就执行中间操作链,并产生结果。
  4. Stream 一旦执行了终止操作,就不能再调用其他中间操作或终止操作了。

1. 使用步骤

  1. 创建 Steam 流
  2. 中间操作

每次处理都会返回一个持有结果的新 Stream ,即中间操作的方法返回值是 Stream 类型的对象。因此中间操作可以是个操作链,可以对数据源的数据进行 n 次处理,但是在终结操作前,并不会执行。

  1. 终止操作

一旦执行终止操作,就执行中间操作链,最终产生结果并结束 Stream。

1.1. 创建 Stream 实例

  1. java8 中的 Collection 接口被扩展,提供了两个获取流的方法:
    1. defaulf Stream<E> stream():返回一个顺序流。
    2. default Stream<E> parallelStream(): 返回一个并行流。
  1. Arrays 的静态方法 stream()可以获取数组流:
  • static <T> Stream<T> stream(T[] array) : 返回一个流。
  • public static IntStream stream(int[] array)
  • public static LongStream stream(long[] array)
  • public static DoubleStream stream(double[] array)
  1. 通过 Stream 的 of()
  • public statc<T> Stream<T> of(T.... values):返回一个流。

eg:

@Test
public void test(){
    Stream<Integer> stream = Stream.of(1,2,3,4,5);
    stream.forEach(System.out::println);
}

1.2. 中间操作

  1. 筛选、切片

方 法

描 述

filter(Predicatep)

接收 Lambda , 从流中排除某些元素

distinct()

筛选,通过流所生成元素的 hashCode() 和 equals() 去除重复元素

limit(long maxSize)

截断流,使其元素不超过给定数量

skip(long n)

跳过元素,返回一个扔掉了前 n 个元素的流。若流中元素不足 n 个,则返回一个空流。与 limit(n) 互补

  1. 映射

方法

描述

map(Function f)

接收一个函数作为参数,该函数会被应用到每个元素上,并将其映射成一个新的元素。

mapToDouble(ToDoubleFunction f)

接收一个函数作为参数,该函数会被应用到每个元素上,产生一个新的 DoubleStream。

mapToInt(ToIntFunction f)

接收一个函数作为参数,该函数会被应用到每个元素上,产生一个新的 IntStream。

mapToLong(ToLongFunction f)

接收一个函数作为参数,该函数会被应用到每个元素上,产生一个新的 LongStream。

flatMap(Function f)

接收一个函数作为参数,将流中的每个值都换成另一个流,然后把所有流连接成一个流

  1. 排序

方法

描述

sorted()

产生一个新流,其中按自然顺序排序

sorted(Comparatorcom)

产生一个新流,其中按比较器顺序排序

1.3. 终止操作

到终止之前,这一串表达式的结果还只是流,因此要通过终止操作将流转换为实际可用的数据。

  1. 匹配与查找

方法

描述

allMatch(Predicate p)

检查是否匹配所有元素

anyMatch(Predicate p)

检查是否至少匹配一个元素

noneMatch(Predicate p)

检查是否没有匹配所有元素

findFirst()

返回第一个元素

findAny()

返回当前流中的任意元素

count()

返回流中元素总数

max(Comparator c)

返回流中最大值

min(Comparator c)

返回流中最小值

forEach(Consumer c)

内部迭代(使用 Collection 接口需要用户去做迭代,称为外部迭代。
相反,Stream API 使用内部迭代——它帮你把迭代做了)

  1. 规约

方法

描述

reduce(T identity, BinaryOperator b)

可以将流中元素反复结合起来,得到一个值。返回 T

reduce(BinaryOperator b)

可以将流中元素反复结合起来,得到一个值。返回 Optional

  1. 收集

方 法

描 述

collect(Collector c)

将流转换为其他形式。接收一个 Collector接口的实现,
用于给Stream中元素做汇总的方法

Collector 接口中方法的实现决定了如何对流执行收集的操作(如收集到 List、Set、Map)。

另外, Collectors 实用类提供了很多静态方法,可以方便地创建常见收集器实例,具体方法与实例如下表:

方法

返回类型

作用

toList

Collector<T, ?, List>

把流中元素收集到List

List<Employee> emps= list.stream().collect(Collectors.toList());

方法

返回类型

作用

toSet

Collector<T, ?, Set>

把流中元素收集到Set

Set<Employee> emps= list.stream().collect(Collectors.toSet());

方法

返回类型

作用

toCollection

Collector<T, ?, C>

把流中元素收集到创建的集合

Collection<Employee> emps =list.stream().collect(Collectors.toCollection(ArrayList::new));

方法

返回类型

作用

counting

Collector<T, ?, Long>

计算流中元素的个数

long count = list.stream().collect(Collectors.counting());

方法

返回类型

作用

summingInt

Collector<T, ?, Integer>

对流中元素的整数属性求和

int total=list.stream().collect(Collectors.summingInt(Employee::getSalary));

方法

返回类型

作用

averagingInt

Collector<T, ?, Double>

计算流中元素Integer属性的平均值

double avg = list.stream().collect(Collectors.averagingInt(Employee::getSalary));

方法

返回类型

作用

summarizingInt

Collector<T, ?, IntSummaryStatistics>

收集流中Integer属性的统计值。如:平均值

int SummaryStatisticsiss= list.stream().collect(Collectors.summarizingInt(Employee::getSalary));

方法

返回类型

作用

joining

Collector<CharSequence, ?, String>

连接流中每个字符串

String str= list.stream().map(Employee::getName).collect(Collectors.joining());

方法

返回类型

作用

maxBy

Collector<T, ?, Optional>

根据比较器选择最大值

Optional<Emp>max= list.stream().collect(Collectors.maxBy(comparingInt(Employee::getSalary)));

方法

返回类型

作用

minBy

Collector<T, ?, Optional>

根据比较器选择最小值

Optional<Emp> min = list.stream().collect(Collectors.minBy(comparingInt(Employee::getSalary)));

方法

返回类型

作用

reducing

Collector<T, ?, Optional>

从一个作为累加器的初始值开始,利用BinaryOperator与流中元素逐个结合,从而归约成单个值

int total=list.stream().collect(Collectors.reducing(0, Employee::getSalar, Integer::sum));

方法

返回类型

作用

collectingAndThen

Collector<T,A,RR>

包裹另一个收集器,对其结果转换函数

int how= list.stream().collect(Collectors.collectingAndThen(Collectors.toList(), List::size));

方法

返回类型

作用

groupingBy

Collector<T, ?, Map<K, List>>

根据某属性值对流分组,属性为K,结果为V

Map<Emp.Status, List<Emp>> map= list.stream().collect(Collectors.groupingBy(Employee::getStatus));

方法

返回类型

作用

partitioningBy

Collector<T, ?, Map<Boolean, List>>

根据true或false进行分区

Map<Boolean,List<Emp>> vd = list.stream().collect(Collectors.partitioningBy(Employee::getManage));
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值