java8新特性------StreamApi

什么是Stream?

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

区别在于 集合操作的是数据,而流是进行计算

Stream特点

  1. Stream   不会自己存储元素
  2. Stream   不会改变源对象,会返回一个带结果的  新的Stream
  3. Stream   操作是延迟执行的。意味着Stream要等到结果的时候才会执行

Stream的使用

一、创建  四种方式

1、通过Collection接口创建

  • default Stream<E> stream():返回一个顺序流
  • default Stream<E> parallelStream():返回一个并行流

2、通过Arrays静态方法stream()创建数组流

  • static <T> Stream<T> stream(T[] array):返回一个T类型的数组流
  • public static IntStream stream(int[] array):返回一个int型的数组流
  • public static LongStream stream(long[] array):返回一个long类型的数组流
  • public static DoubleStream stream(double[] array):返回一个double类型的数组流

3、通过值创建流

  • public static <T>Stream <T> of(T... values):接收任意数量的参数返回一个T类型的流

4、通过函数创建流

  • 迭代方式:public static <T> Stream<T>iterate(final T seed,final UnaryOperator<T>f)
  • 生成方式:public static <T>Stream <T>generate(Supplier<T> s):

二、中间操作  三种

筛选和切片

方法描述
filter(Predicate p)接收Lambda,从流中排除某些元素
distinct()筛选,通过流生成元素的hashCode()和equals()方法去除重复元素
limit(long maxSize)截断流,使元素不超过给定的数量
skip(long n)

跳过元素返回一个扔掉了前n个元素的流。若流中元素不足n个,

则返回一个空流。与limit(n)互补

映射

 

方法描述
map(Function f)

接收一个函数作为参数,该函数会被应用到每个元素上,

并将其映射成一个新元素

mapToDouble(ToDoubleFunction f)

接收一个函数作为参数,该函数会被应用到每个元素上

产生一个新的DoubleStream

mapToInt(ToIntFunction f)

接收一个函数作为参数,该函数会被应用到每个元素上

产生一个新的IntStream

mapToLong(ToLongFunction f)

接收一个函数作为参数,该函数会被应用到每个元素上

产生一个新的LongStream

floatMap(Function f)

接收一个函数作为参数

将流中的每一个值都换成另一个流

然后把所有流连接成一个流

排序

方法描述
sorted()产生一个新流,按照自然顺序排序
sorted(Comparator comp)产生一个新流,按照比较器排序

三、终止操作(终端操作) 

查找和匹配

方法描述
allMatch(Predicate p)检查是否匹配所有元素
anyMatch(Predicate p)检查是否至少匹配一个元素
noneMatch(Predicate p)检查是否没有匹配所有元素
findFirst()返回第一个元素
findAny()返回流中任意的元素
count()返回流中元素总数
max(Comparator c)返回流中最大值
min(Comparator c)返回流中最下值
forEach(Consumer c)

内部迭代使用Collection接口需要用户去迭代,称为内部迭代

相反,StreamApi使用内部迭代--StreamAPI帮你把迭代做了

归约

方法描述
reduce(T iden,BinaryOperator b)可以将流中元素
reduce(BinaryOperator b)

可以将流中的元素反结合起来得到一个值。

返回 Optional<T>

收集

方法描述
collect(Collector c)将流转换为其它形式。接收一个Collector接口的实现,用于给Stream中元素做汇总的方法

Collector 接口中方法的实现决定了如何对流执行收集操作(如收集到 List、Set、Map)。但是 Collectors 实用类提供了很多静态方法,可以方便地创建常见收集器实例,具体方法与实例如下表:

方法返回类型作用示例
toListList<T>把流中元素收集到ListList<Employee> emps= list.stream().collect(Collectors.toList());
toSetSet<T>把流中元素收集到SetSet<Employee> emps= list.stream().collect(Collectors.toSet());
toCollectionCollection<T>把流中元素收集到创建的集合

Collection<Employee>emps=list.stream()

.collect(Collectiors.toCollection(ArrayList::new));

counting  Long 计算流中元素的个数

long count = list.stream()

.collect(Collectors.counting());

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

inttotal=list.stream()

.collect(Collectors.summingInt(Employee::getSalary);

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

doubleavg= list.stream()

.collect(Collectors.averagingInt(Employee::getSalary));

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

IntSummaryStatisticsiss= list.stream()

.collect(Collectors.summarizingInt(Employee::getSalary));

joinin String 连接流中每个字符串

String str= list.stream().map(Employee::getName)

.collect(Collectors.joining());

maxBy Optional<T>根据比较器选择最大值

Optional<Emp>max= list.stream()

.collect(Collectors.maxBy(comparingInt(Employee::getSalary)));

minBy Optional<T>根据比较器选择最小值

Optional<Emp> min = list.stream()

.collect(Collectors.minBy(comparingInt(Employee::getSalary)));

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

inttotal=list.stream()

.collect(Collectors.reducing(0,Employee::getSalar,Integer::sum)

collectingAndThen 转换函数返回的类型包裹另一个收集器,对其结
果转换函数

inthow= list.stream()

.collect(Collectors.collectingAndThen(Collectors.toList(),List::size));

groupingBy Map<K, List<T>>根据某属性值对流分组,属
性为K,结果为V
Map<Emp.Status, List<Emp>> map= list.stream()
.collect(Collectors.groupingBy(Employee::getStatus))
partitioningBy Map<Boolean,List<T>>根据true或false进行分区

Map<Boolean,List<Emp>>vd= list.stream()

.collect(Collectors.partitioningBy(Employee::getManage));

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

诗水人间

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值