Java知识点-Stream流

Stream流的中间方法

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

Stream流的终结方法

  • parseInt转换数据类型
  • 收集到Map集合,数据不能重复,不然会报错

练习

java 复制代码
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class Test1 {
    public static void main(String[] args) {
        /*定义一个集合,并添加一些整数
        过滤奇数,只留下偶数。
        并将结果保存起来 1,2,3,4,5,6,7,8,9,10

         */
        //1.定义一个集合
        ArrayList<Integer> list = new ArrayList<Integer>();

        //2.添加一些整数
        Collections.addAll(list, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
        //3.过滤奇数,如果是偶数则保留
        List<Integer> newList = list.stream().filter(n -> n % 2 == 0).collect(Collectors.toList());
        System.out.println(newList);
    }
}
java 复制代码
import java.util.ArrayList;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public class Test2 {
    public static void main(String[] args) {
        /*
        练习:
        创建一个ArrayList集合,并添加以下字符串,字符串中前面是姓名,后面是年龄"zhangsan,23""lisi,24""wangwu,25"
        保留年龄大于等于24岁的人,并将结果收集到Map集合中,姓名为键,年龄为值
        */

        //1.创建一个ArrayList集合
        ArrayList<String> list = new ArrayList<>();
        //2.添加字符
        list.add("zhangsan,23");
        list.add("lisi,24");
        list.add("wangwu,25");
        //3.保留年龄大于等于24岁的人
        /*list.stream().filter(s->Integer.parseInt(s.split(",")[1])>=24)
                .collect(Collectors.toMap(new Function<String, String>() {
                    @Override
                    public String apply(String s) {
                        return s.split(",")[0];
                    }
                }, new Function<String, Integer>() {
                    @Override
                    public Integer apply(String s) {
                        return Integer.parseInt(s.split(",")[1]);
                    }
                }));*/
        Map<String, Integer> map = list.stream()
                .filter(s -> Integer.parseInt(s.split(",")[1]) >= 24)
                .collect(Collectors.toMap(
                        s -> s.split(",")[0],
                        s -> Integer
                                .parseInt(s.split(",")[1])));
        System.out.println(map);
    }
}
  • 可以先写匿名内部类,再写toMap
java 复制代码
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Test3 {
    public static void main(String[] args) {
        /*现在有两个ArrayList集合,分别存储6名男演员的名字和年车龄以及6名女演员的名字和年龄
        姓名和年龄中间用逗号隔开。
        比如:张三,23
        要求完成如下的操作:
        1,男演员只要名字为3个字的前两人
        2,女演员只要姓杨的,并且不要第一个
        3,把过滤后的男演员姓名和女演员姓名合并到一起
        4,将上一步的演员姓名封装成Actor对象。
        5,将所有的演员对象都保存到List集合中。
        备注:演员类Actor,属性有:name,age
        男演员:"蔡坤坤,24","叶胸咸,23","刘不甜,22","吴签,24","谷嘉,30","肖梁梁,27
        女演员:"赵小颖,35","杨颖,36","高元元,43","张天天,31","刘诗,35","杨小幂,33"
        */
        //1.创建两个集合
        ArrayList<String> manList = new ArrayList<String>();
        ArrayList<String> womenList = new ArrayList<>();

        //2.添加数据
        Collections.addAll(manList, "蔡坤坤,24", "叶胸咸,23", "刘不甜,22", "吴签,24", "谷嘉,30", "肖梁梁,27");
        Collections.addAll(womenList, "赵小颖,35", "杨颖,36", "高元元,43", "张天天,31", "刘诗,35", "杨小幂,33");
        //3.男演员只要名字为3个字的前两人
        Stream<String> stream1 = manList.stream().filter(s -> s.split(",")[0].length() == 3)
                .limit(2);

        //4.女演员只要姓杨的,并且不要第一个
        Stream<String> stream2 = womenList.stream().filter(s -> s.split(",")[0].startsWith("杨"))
                .skip(1);

        //5.将上一步的演员姓名封装成Actor对象。
        /*Stream.concat(stream1,stream2).map(new Function<String,Actor>() {
            @Override
            public Actor apply(String s) {
                String naem = s.split(",")[0];
                int age = Integer.parseInt(s.split(",")[1]);
                return new Actor(naem, age);
            }
        }).forEach(s->System.out.println(s));*/
        List<Actor> list = Stream.concat(stream1, stream2)
                .map(s -> new Actor(s.split(",")[0], Integer.parseInt(s.split(",")[1])))
                .collect(Collectors.toList());//将所有元素收集到List当中
        System.out.println(list);
    }
}
相关推荐
非凡ghost5 小时前
猫眼浏览器(Chrome内核增强版浏览器)官方便携版
前端·网络·chrome·windows·软件需求
熊文豪11 小时前
Windows安装RabbitMQ保姆级教程
windows·分布式·rabbitmq·安装rabbitmq
搬砖的小码农_Sky11 小时前
Windows操作系统上`ping`命令的用法详解
运维·网络·windows
Kiri霧18 小时前
Rust模式匹配详解
开发语言·windows·rust
程序设计实验室20 小时前
使用命令行删除 Windows 网络映射驱动器
windows
用户31187945592181 天前
Windows 电脑安装 XTerminal 1.25.1 x64 版(带安装包下载关键词)
windows
Logintern091 天前
windows如何设置mongodb的副本集
数据库·windows·mongodb
Chandler241 天前
一图掌握 操作系统 核心要点
linux·windows·后端·系统
ajassi20001 天前
开源 C# 快速开发(十七)进程--消息队列MSMQ
windows·开源·c#
Python私教1 天前
5分钟上手 MongoDB:从零安装到第一条数据插入(Windows / macOS / Linux 全平台图解)
windows·mongodb·macos