一.stream介绍

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

  stream 中的操作有filter map limt sorted collect

二.生成stream的方式

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

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

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

三.stream中的操作

 1.filter distinct skip limit 操作

  1. public class StreamOperator {
  2.  
  3. public static void main(String[] args) {
  4.  
  5. List<Integer> asList = Arrays.asList(1, 2, 3, 4, 5, 7, 6, 4, 2, 1, 8, 6, 9, 3);
  6.  
  7. // 1.filter 过滤偶数 将流按一定条件进行过虑 返回过虑后符合规则的stream
  8. List<Integer> filterList = asList.stream().filter(i -> i % 2 == 0).collect(Collectors.toList());
  9. System.out.println(filterList);
  10.  
  11. //2.distinct 去重
  12. List<Integer> disList = asList.stream().distinct().collect(Collectors.toList());
  13. System.out.println(disList);
  14.  
  15. //3.skip 跳过n个元素
  16. List<Integer> skipList = asList.stream().skip(5).collect(Collectors.toList());
  17. System.out.println(skipList);
  18.  
  19. //4.limit 极限值是几个(个人理解,也就是最多取几个元素,如果limit取的个数超过了stream中元素的最大个数,会取到流中的全部元素)
  20. List<Integer> limList = asList.stream().limit(5).collect(Collectors.toList());
  21. System.out.println(limList); }
  22. }

2.map flatmap操作

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

3.sort collect

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

四.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. ubuntu 16.04安装gitlab,然后汉化

    1 前期准备 电脑配置:windows7 ,内存8GB以上(因为有4GB左右要分配给虚拟机中的ubuntu) 虚拟机:VBOX Linux系统:ubuntu16.04 64bit 2 Gitlab的搭 ...

  2. js毫秒数转换为具体日期

    [1].毫秒数转换为具体日期 function getMyDate(str) {    var oDate = new Date(str),    oYear = oDate.getFullYear( ...

  3. LeetCode 44. 通配符匹配(Wildcard Matching)

    题目描述 给定一个字符串 (s) 和一个字符模式 (p) ,实现一个支持 '?' 和 '*' 的通配符匹配. '?' 可以匹配任何单个字符. '*' 可以匹配任意字符串(包括空字符串). 两个字符串完 ...

  4. No hash for parcel CDH-XXX.parcel.torrent

    在安装 CDH 时,到 Install Parcels 这一步,分发 Parcels 一直过不去,界面一直报 Failure due to stall on seeded torrent,查看日志一直 ...

  5. code备忘

    按空白符分隔(正则) String[] split = line.trim().split("\\s+");

  6. zookeeper备忘

    ZooInspector https://issues.apache.org/jira/secure/attachment/12436620/ZooInspector.zip 参考:https://b ...

  7. 2017-12-3 Crontab(字符串处理)

    Crontab 哈哈本人的不及格代码(暂留): #include<iostream> #include<queue> #include<cmath> #includ ...

  8. [MyBatis]向MySql数据库插入一千万条数据 批量插入用时6分 之前时隐时现的异常不见了

    本例代码下载:https://files.cnblogs.com/files/xiandedanteng/InsertMillionComparison20191012.rar 这次实验的环境仍然和上 ...

  9. [log4j]Error:The method getLogger(String) in the type Logger is not applicable for the arguments

    原因:本该导入import org.apache.log4j.Logger; 结果成了import java.util.logging.Logger; 如果硬把private static Logge ...

  10. UM概述

    历史 UML创始于1994年10月,主要创始人Grady Booch.Jim Rumbaugh和Ivar Jacobson. UML(Unified modeling language统一建模语言) ...