java--Stream流

简介: java--学习笔记

Stream流

1、Stream的作用

结合了Lambda表达式,简化集合、数组的操作

2、Stream流的使用步骤
  • 先得到一条Stream流,并把数据放上去
  • 利用Stream流中的API进行各种操作

Snipaste_2023-08-23_09-24-22.png

单列集合

public class Stream01 {
   
   
    public static void main(String[] args) {
   
   
        ArrayList<String> arrayList = new ArrayList<>();

        Collections.addAll(arrayList,"a","b","c","d");

        arrayList.stream().forEach(s -> System.out.println(s));
    }
}

双列集合

注意:双列集合不能使用stream流,需要先转成单列集合

public class Stream02 {
   
   
    public static void main(String[] args) {
   
   
        HashMap<String, Integer> hashMap = new HashMap<>();

        hashMap.put("A",1);
        hashMap.put("B",2);
        hashMap.put("C",3);

        Set<String> strings = hashMap.keySet();

        strings.stream().forEach(s -> System.out.println(hashMap.get(s)));
    }
}

数组

public class Stream03 {
   
   
    public static void main(String[] args) {
   
   
        int[] arr = {
   
   1,2,3,4,5,6};

        Arrays.stream(arr).forEach(s -> System.out.println(s));
    }
}

零散的数据

注意:需要是同种数据类型

public class Stream04 {
   
   
    public static void main(String[] args) {
   
   
        Stream.of(1,2,4,5,6,7).forEach(s -> System.out.println(s));
    }
}
3、Stream的中间方法

Snipaste_2023-08-23_09-50-56.png

注意:

  • 中间方法,返回新的Stream流,原来的Stream流只能使用一次,建议使用链式编程
  • 修改Stream流中的数据,不会影响原来集合或者数组中的数据

过滤 filter

public class Stream05 {
   
   
    public static void main(String[] args) {
   
   
        ArrayList<String> arrayList = new ArrayList<>();
        Collections.addAll(arrayList,"张三","张子","小林","张天","刘十三");

        //留下张开头的,其余的过滤
        arrayList.stream().filter(s -> s.startsWith("张")).forEach(s -> System.out.println(s));

    }
}

获取前几个数据 limit

public class Stream05 {
   
   
    public static void main(String[] args) {
   
   
        ArrayList<String> arrayList = new ArrayList<>();
        Collections.addAll(arrayList,"张三","张子","小林","张天","刘十三");

        //获取前几个数据 limit
        arrayList.stream().limit(3).forEach(s -> System.out.println(s));

    }
}

跳过前几个数据 skip

public class Stream05 {
   
   
    public static void main(String[] args) {
   
   
        ArrayList<String> arrayList = new ArrayList<>();
        Collections.addAll(arrayList,"张三","张子","小林","张天","刘十三");

        //跳过前几个数据 skip
        arrayList.stream().skip(3).forEach(s -> System.out.println(s));

    }
}

去重 distinct

public class Stream06 {
   
   
    public static void main(String[] args) {
   
   
        ArrayList<String> arrayList = new ArrayList<>();

        Collections.addAll(arrayList,"小林","小吴","小林","小天","小吴","小沈");

        arrayList.stream().distinct().forEach(s -> System.out.println(s));
    }
}

合并流 concat

public class Stream07 {
   
   
    public static void main(String[] args) {
   
   
        ArrayList<String> arrayList01 = new ArrayList<>();
        ArrayList<String> arrayList02 = new ArrayList<>();

        Collections.addAll(arrayList01,"小林","小吴");
        Collections.addAll(arrayList02,"小沈","小天");

        Stream.concat(arrayList01.stream(),arrayList02.stream()).forEach(s -> System.out.println(s));
    }
}

类型转换 map

public class Stream07 {
   
   
    public static void main(String[] args) {
   
   
        ArrayList<String> arrayList = new ArrayList<>();

        Collections.addAll(arrayList,"小林-15","小吴-14");

        //只获取年龄
        arrayList.stream().map(s -> Integer.parseInt(s.split("-")[1])).forEach(s -> System.out.println(s));

    }
4、Stream的终结方法

Snipaste_2023-08-23_10-33-28.png

遍历 forEach

public class Stream08 {
   
   
    public static void main(String[] args) {
   
   

        ArrayList<String> arrayList = new ArrayList<>();

        Collections.addAll(arrayList,"小林","小吴","小沈","小天");

        arrayList.stream().forEach(s -> System.out.println(s));
    }
}

count 统计

public class Stream08 {
   
   
    public static void main(String[] args) {
   
   

        ArrayList<String> arrayList = new ArrayList<>();

        Collections.addAll(arrayList,"小林","小吴","小沈","小天");

        System.out.println(arrayList.stream().count());
    }
}

存放到数组 toArray

public class Stream08 {
   
   
    public static void main(String[] args) {
   
   

        ArrayList<String> arrayList = new ArrayList<>();

        Collections.addAll(arrayList,"小林","小吴","小沈","小天");

        String string = Arrays.toString(arrayList.stream().toArray(value -> new String[value]));
        System.out.println(string);
    }
}
5、收集方法collect
5.1、收集到list集合
public class Stream09 {
   
   
    public static void main(String[] args) {
   
   
        /**
         *  collect(Collector collect)   收集流中的数据,放在集合中
         */

        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list,"刘十三-男-19","王莹莹-女-18","李二壮-男-18","大牛-女-19");

        /**
         * 收集list集合中所有男性
         */
        List<String> sexList = list.stream().filter(s -> s.split("-")[1].equals("男"))
                .collect(Collectors.toList());

        System.out.println(sexList);
    }
}
5.2、收集到Set集合

注意:收集到set中会去掉重复数据

public class Stream09 {
   
   
    public static void main(String[] args) {
   
   
        /**
         *  collect(Collector collect)   收集流中的数据,放在集合中
         */

        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list,"刘十三-男-19","王莹莹-女-18","李二壮-男-18","大牛-女-19");

        /**
         * 收集list集合中所有男性
         */
        Set<String> sexSet = list.stream().filter(s -> s.split("-")[1].equals("男"))
                .collect(Collectors.toSet());

        System.out.println(sexSet);
    }
}
5.3、收集到Map集合
public class Stream09 {
   
   
    public static void main(String[] args) {
   
   
        /**
         *  collect(Collector collect)   收集流中的数据,放在集合中
         */

        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list,"刘十三-男-19","王莹莹-女-18","李二壮-男-18","大牛-女-19");

        /**
         * 收集list集合中所有男性
         * 收集到map集合
         * 键为姓名,值为年龄
         */

        Map<String, String> map = list.stream().filter(s -> s.split("-")[1].equals("男"))
                .collect(Collectors.toMap(s -> s.split("-")[0], o -> o.split("-")[2]));

        System.out.println(map);
    }
}
6、Stream综合练习
public class StreamDemo01 {
   
   
    public static void main(String[] args) {
   
   
        /**
         * 1、定义两个集合存储6名男演员,6名女演员,用逗号隔开,如:张三,23
         * 2、男演员只要名字为3个字的前两人
         * 3、女演员只要姓杨的,不要第一个
         * 4、过滤后合并在一起
         * 5、将过滤的演员封装成Actor对象
         * 6、将所有演员对象保存在list集合中
         * 备注:演员类 Actor,属性有name,age
         */

        //定义两个Arrlist集合
        ArrayList<String> arrayListMan = new ArrayList<>();
        ArrayList<String> arrayListWoman = new ArrayList<>();

        //存放集合
        ArrayList<Actor> actors = new ArrayList<>();

        //添加数据
        Collections.addAll(arrayListMan,"张三,23","李四,24","王五,25","赵六哇,26","陈七公,27","宋八家,28");
        Collections.addAll(arrayListWoman,"小红,23","杨吴,24","杨张,25","杨李,26","小林,27","小江,28");

        Stream.concat(
                arrayListMan.stream().filter(s -> s.split(",")[0].length() >= 3).limit(2),
                arrayListWoman.stream().filter(s -> s.split(",")[0].startsWith("杨")).skip(1)
        ).forEach(s -> actors.add(new Actor(s.split(",")[0],s.split(",")[1])));

        System.out.println(actors);
    }
}
相关文章
|
人工智能 自然语言处理 JavaScript
AI编程助手工具,走过路过别错过
随着人工智能技术的发展,越来越多的编程助手工具涌现出来,帮助程序员更高效地完成编码任务。最新的AI编程助手工具有哪些呢?本文将为您介绍几款备受关注的工具。
|
Kubernetes 安全 Go
对于阿里开源混沌工程工具chaosblade-box-agent心跳报错问题的分析与解决
摘要: 本文记录了一个由chaosblade-box平台后台发现的偶发的chaosblade-box-agent不发送心跳的问题,从报错日志入手,结合chaosblade-box-agent源码进行分析,最终解决问题并修复打包的过程。
671 7
|
算法 编译器 程序员
成为C++重载大师:深入理解重载决议
成为C++重载大师:深入理解重载决议
211 0
|
网络协议 安全 应用服务中间件
采用云虚拟主机搭建个人网页并开启ssl认证
采用云虚拟主机搭建个人网页并开启ssl认证
云·企业官网
一对一设计师定制设计,不满意全额退款!
云·企业官网
广西实施金融人才“领航”“英才”“远航”三大人才计划
6月18日,在解读《加快建设面向东盟的金融开放门户若干措施》新闻发布会上,广西地方金融监管局局长范世祥表示,按照分类分层、梯队培养的思路,广西将实施“领航”“英才”“远航”三大金融人才培养工程。
|
6天前
|
弹性计算 关系型数据库 微服务
基于 Docker 与 Kubernetes(K3s)的微服务:阿里云生产环境扩容实践
在微服务架构中,如何实现“稳定扩容”与“成本可控”是企业面临的核心挑战。本文结合 Python FastAPI 微服务实战,详解如何基于阿里云基础设施,利用 Docker 封装服务、K3s 实现容器编排,构建生产级微服务架构。内容涵盖容器构建、集群部署、自动扩缩容、可观测性等关键环节,适配阿里云资源特性与服务生态,助力企业打造低成本、高可靠、易扩展的微服务解决方案。
1151 3
|
5天前
|
机器学习/深度学习 人工智能 前端开发
通义DeepResearch全面开源!同步分享可落地的高阶Agent构建方法论
通义研究团队开源发布通义 DeepResearch —— 首个在性能上可与 OpenAI DeepResearch 相媲美、并在多项权威基准测试中取得领先表现的全开源 Web Agent。
758 11
|
15天前
|
人工智能 运维 安全