JDK1.8新特性之stream流基础

本文详细介绍了Java中Stream流的三种创建方式,以及常用的流操作如distinct、filter、map、sorted、max/min、groupingBy,同时讨论了parallelStream的并行处理功能。
摘要由CSDN通过智能技术生成

1.生成stream三中方式

1.1List<Student>list=new ArrayList ();

Stream stream1=list.stream();

1.2Stream stream2=Stream.of("hello","hi","hello");

1.3Stream stream3=Arrays.asList("hello","hi","good").stream();

 

2.stram流常用函数

2.1distinct:去从

lstream2.distint().foreach(System.out.println;)

结果:

hello

hi

cedc1aa25b614a2b88e9afda5e06cf19.png 

2.2filter:过滤(筛选)

List <Student>=stream1.filter(s->s.getAge==10).collect(collector.asList())

 

2.3 map():

2.3.1选择字段,生成一个新的List集合

List<String> lists=stream.map(Student::getName).collect(collector.asList())

2.3.2对流中的对象的成员变量进行设置

 List <Student> stus=stream.map(s->{

s.setAge(12);}

).collect.collector.asList());

53954bf75db74bfebd5e5e0080f63600.png

 

2.4.sorted():排序

2.4.1默认升序

a7d6edc1acbe4b0bb3254e2cce9a21f0.png 

2.4.2自定义排序

按照学生的年龄逆序排序

stream1.sort(Comparator.comparing(Student::getAge()).reverce());

 

2.5 max(),min()求最大最小值

b1fa7864a5cd41f6a5677c12839cc2f8.png

 

2.6 groupingBy()分组

例如:按照学生年龄进行分组:List<Map<String,Student>> listmaps=stream1.collect(Collector.gruopingBy(Student::getAge ());

分组统计:

Map<String,Intger> listmaps=stream1.collect(Collectors.gruopingBy(Student::getAge (),Collectors.counting()));

分组求和:Map<String,Long> listmaps=stream1.collect(Collectors.gruopingBy(Student::getName (),Collectors.summingLong(Student::getAge ()));

 

3.stream流的升级paralleStream (并行流)

对集合中的元素进行并行处理

List <Student> stus=stream.map(s->{

s.setAge(12);}

).collect.collector.asList());

List <Student> stus=list.parralleStream.map(s->{

s.setAge(12);}

).collect.collector.asList());

 

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

莳光.

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

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

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

打赏作者

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

抵扣说明:

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

余额充值