一.stream介绍

  stream 是jdk 一个加强的api操作,翻译过来就是流的意思,主要是对Collection 集合的一些操作,流一但生成就会有方向性,类似于自来水管的水流一样,不可以重复使用。

  stream 中的操作有filter map limt sorted collect

二.生成stream的方式

  集合.stream(); − 为集合创建串行流

  集合.parallelStream()  − 为集合创建并行流

//列举一些常见的创建stream的方法
List<Apple> apples = Arrays.asList(new Apple("red", 120),new Apple("green", 170),new Apple("yellow", 200)); Stream<Apple> stream = apples.stream(); Stream<Apple> stream2 = Stream.of(new Apple("red", 120),new Apple("green", 170),new Apple("yellow", 200)); Apple arr [] = {new Apple("red", 120),new Apple("green", 170),new Apple("yellow", 200)};
Stream<Apple> stream3 = Arrays.stream(arr);

三.stream中的操作

 1.filter distinct skip limit 操作

public class StreamOperator {

    public static void main(String[] args) {

        List<Integer> asList = Arrays.asList(1, 2, 3, 4, 5, 7, 6, 4, 2, 1, 8, 6, 9, 3);

        // 1.filter 过滤偶数  将流按一定条件进行过虑  返回过虑后符合规则的stream
List<Integer> filterList = asList.stream().filter(i -> i % 2 == 0).collect(Collectors.toList());
System.out.println(filterList); //2.distinct 去重
List<Integer> disList = asList.stream().distinct().collect(Collectors.toList());
System.out.println(disList); //3.skip 跳过n个元素
List<Integer> skipList = asList.stream().skip(5).collect(Collectors.toList());
System.out.println(skipList); //4.limit 极限值是几个(个人理解,也就是最多取几个元素,如果limit取的个数超过了stream中元素的最大个数,会取到流中的全部元素)
List<Integer> limList = asList.stream().limit(5).collect(Collectors.toList());
System.out.println(limList); }
}

2.map flatmap操作

public class StreamTest {
public static void main(String[] args) { //map 操作
List<Apple> apples =Arrays.asList(
new Apple("green", 150),new Apple("red", 170),new Apple("yellow", 190),
new Apple("blue", 210)); //map 操作是将stream 通过map操作返回符合map规则的stream
     // map 与filter 的区别
     //  map是一个Function 即传和一个 T 返回一个 R 类似于get操作 如果流中有元素符合map的操作条件,会将得到的R存入到stream中,会改变流中存储的元素
     //  filter 是一个 Predicate 即传入一个 T 返回的是bollean 是用来做判断的,如果流中的某个元素符合filter的条件,就会存入到stream中,不符合的将被过滤掉,不会改变流中存储的元素 Stream<String> map = apples.stream().map(Apple::getColor); //flatmap(扁平化)
String [] arr = {"hello","world"};
//{h,e,l,l,o} {w,o,r,l,d}
Stream<String[]> streamMap = Arrays.stream(arr).map(w->w.split("")); //[h,e,l,l,o,w,o,r,l,d] 由flatMap的参数可知,传入的参数T最后都合成一个只要是继承stream的类即可,因此将streamMap流数组传入flatMap会得到1个stream流
// flatMap(Function<? super T, ? extends Stream<? extends R>> mapper)
// R apply(T t);
Stream<String> flatMap = streamMap.flatMap(Arrays::stream);
flatMap.distinct().forEach(System.out::print);
}
}

3.sort collect

public class StreamTest {
public static void main(String[] args) {
List<Apple> apples =Arrays.asList(
new Apple("green", 150),new Apple("red", 170),new Apple("yellow", 190),
new Apple("blue", 210));    //sorted 是按颜色的字母顺序来排序
     //collect 是将流转换成相对应的集合
List<Apple> collect = apples.stream().sorted(Comparator.comparing(Apple::getColor)).collect(Collectors.toList()); }
}

四.stream 的并行操作

  将集合转换成并行流后之后,其它操作与串行流相同

  并行流可以通过将程序休眠,通过jconsole 工具查看

jdk1.8 -- stream 的使用的更多相关文章

  1. 使用jdk1.8 stream特性对参数名称进行排序

    在对外对接的时候,通常会碰到签名方式, 然后签名的时候,要求按照参数名称进行排序. 比如参数为 c=22&a=1, 需要将结果排序为a=1&c=22, 然后再进行别的运算. 可以使用j ...

  2. JDK1.8 Stream

    Java 8 API添加了一个新的抽象称为流Stream,可以让你以一种声明的方式处理数据. Stream 使用一种类似用 SQL 语句从数据库查询数据的直观方式来提供一种对 Java 集合运算和表达 ...

  3. jdk1.8 Stream 特性总结

    不是数据结构 它没有内部存储,它只是用操作管道从 source(数据结构.数组.generator function.IO channel)抓取数据. 它也绝不修改自己所封装的底层数据结构的数据.例如 ...

  4. java代码之美(12)---CollectionUtils工具类

    java代码之美(12)---CollectionUtils工具类 这篇讲的CollectionUtils工具类是在apache下的, 而不是springframework下的CollectionUt ...

  5. Java中List集合去除重复数据的六种方法

    1. 循环list中的所有元素然后删除重复 public static List removeDuplicate(List list) { for ( int i = 0 ; i < list. ...

  6. java代码(12) ---CollectionUtils工具类

    CollectionUtils工具类 CollectionUtils工具类是在apache下的,而不是springframework下的CollectionUtils 个人觉得在真实项目中Collec ...

  7. ForkJoinPool大型图文现场(一阅到底 vs 直接收藏)

    知识回顾 并发工具类我们已经讲了很多,这些工具类的「目标」是让我们只关注任务本身,并且忽视线程间合作细节,简化了并发编程难度的同时,也增加了很多安全性.工具类的对使用者的「目标」虽然一致,但每一个工具 ...

  8. Java进阶篇之十五 ----- JDK1.8的Lambda、Stream和日期的使用详解(很详细)

    前言 本篇主要讲述是Java中JDK1.8的一些新语法特性使用,主要是Lambda.Stream和LocalDate日期的一些使用讲解. Lambda Lambda介绍 Lambda 表达式(lamb ...

  9. JDK1.8新特性——Stream API

    JDK1.8新特性——Stream API 摘要:本文主要学习了JDK1.8的新特性中有关Stream API的使用. 部分内容来自以下博客: https://blog.csdn.net/icarus ...

随机推荐

  1. B/S架构大文件上传问题

    核心原理: 该项目核心就是文件分块上传.前后端要高度配合,需要双方约定好一些数据,才能完成大文件分块,我们在项目中要重点解决的以下问题. * 如何分片: * 如何合成一个文件: * 中断了从哪个分片开 ...

  2. [pytorch] 自定义激活函数中的注意事项

    如何在pytorch中使用自定义的激活函数? 如果自定义的激活函数是可导的,那么可以直接写一个python function来定义并调用,因为pytorch的autograd会自动对其求导. 如果自定 ...

  3. abstract Factory pattern

    1,注意静态工厂(简单工厂模式).工厂方法.抽象工厂的区别 静态工厂是根据客户端传入的参数,使用工厂类来创建相应的产品接口的具体实现子类对象.比如,需要需要创建一个工具类,该工具类是为了调用外部系统, ...

  4. python中list不能做索引

    先看python中内置的list不能作为字典的key. 可将list或者ndarray转化为tuple再做索引. list不能进行hash: import numpy as np a1 = np.ar ...

  5. Install chocolatey

    Requirements Windows 7+ / Windows Server 2003+ PowerShell v2+ .NET Framework 4+ (the installation wi ...

  6. Linux中soft nproc 、soft nofile和hard nproc以及hard nofile配置

    Linux中soft nproc .soft nofile和hard nproc以及hard nofile配置 "soft" 和 "hard" 的区别 soft ...

  7. Navicat 12 for MySQL最新版激活(注册机)(转)(亲测有效)

    Navicat 12 for MySQL最新版激活(注册机)(转)(亲测有效) 一.总结 一句话总结: 1.卸载自己机器上面的Navicat,安装下载的包里面的Navicat安装包,不然可能不行 2. ...

  8. moogdb操作

    本文转载自 https://my.oschina.net/kakakaka/blog/347954 首先,下载mongdb对JAVA的支持,点击这里下载驱动包,这里博主下载的是2.10.1版. mon ...

  9. 中间件 | kafka简介、使用场景、设计原理、主要配置及集群搭建

    开源Java学习 公众号 一.入门 1.简介 Kafka is a distributed,partitioned,replicated commit logservice.它提供了类似于JMS的特性 ...

  10. C++ STL——map和multimap

    目录 一 map和multimap 注:原创不易,转载请务必注明原作者和出处,感谢支持! 一 map和multimap map相对于set的区别:map具有键值和实值,所有元素根据键值自动排序.pai ...