一.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. Python序列构成的数组

    1.内置序列类型 容器序列:list,tuple,collections.deque (能存放不同类型) 扁平序列:str,bytes,bytearray,memoryview,array.array ...

  2. LA 6972 Domination

    6972 Domination Edward is the headmaster of Marjar University. He is enthusiastic about chess and of ...

  3. use potato

  4. 【csp模拟赛2】 爆搜 方格加数

    [题目描述] xyz1048576正在玩一个关于矩阵的游戏. 一个n*m的矩阵,矩阵中每个数都是[1,12]内的整数.你可以执行下列两个操作任意多次: (1)指定一行,将该行所有数字+1. (2)指定 ...

  5. Linux版本内核及安装后的简单操作命令介绍

    一.Linux的版本与内核 1.Linux发行版 Linux发行版= Linux内核+应用程序 Redhat,CentOS,Ubuntu,Suse,红旗,Mint,Fedora CentOS:社区版操 ...

  6. Leetcode题目104.二叉树的最大深度(DFS+BFS简单)

    题目描述: 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,null,null, ...

  7. OUC_Summer Training_ DIV2_#13 723afternoon

    A - Shaass and Oskols Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I ...

  8. koa 项目实战(六)注册接口加密

    1.创建工具类 根目录/config/tools.js const bcrypt = require('bcryptjs'); const tools = { enbcrypt(password) { ...

  9. storm java.io.NotSerializableException

    今天编写一个storm的topology,bolt的逻辑跟之前的类似. 为了减少重复代码,我建了个抽象基类,存放bolt的公共逻辑,设计了几个abstract方法,不同的逻辑部分由子类实现. 基类日志 ...

  10. leetcode 日常清单

    a:excellent几乎一次ac或只有点小bug很快解决:半年后再重刷: b:经过艰难的debug和磕磕绊绊或者看了小提示才刷出来: c:经过艰难的debug没做出来,看答案刷的: 艾宾浩斯遗忘曲线 ...