Stream流

简介: Stream流

 数据准备

创建实体类Author

package com.zsh.stream.domain.po;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import java.util.List;
@Data
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode// 用于后期的去重使用
public class Author {
    // id
    private Long id;
    // 姓名
    private String name;
    // 年龄
    private Integer age;
    // 简介
    private String intro;
    // 作品
    private List<Book> books;
}

image.gif

创建实体类Book

package com.zsh.stream.domain.po;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode// 用于后期的去重使用
public class Book {
    // id
    private Long id;
    // 书名
    private String name;
    // 分类
    private String category;
    // 评分
    private Integer score;
    // 简介
    private String Intro;
}

image.gif

创建测试类

package com.zsh.stream.demo;
import com.zsh.stream.domain.po.Author;
import com.zsh.stream.domain.po.Book;
import java.util.*;
import java.util.stream.Stream;
public class StreamDemo {
    /**
     * 数据准备
     * @return
     */
    private static List<Author> getAuthors() {
        // 数据初始化
        Author author1 = new Author(1L, "开朗觉觉", 16, "30年超级Java开发工程师", null);
        Author author2 = new Author(1L, "开朗觉觉", 16, "30年超级Java开发工程师", null);
        Author author3 = new Author(3L, "小美", 17, "10年超级Java开发工程师", null);
        Author author4 = new Author(4L, "觉觉", 19, "5年超级Java开发工程师", null);
        // 书籍列表
        ArrayList<Book> books1 = new ArrayList<>();
        ArrayList<Book> books2 = new ArrayList<>();
        ArrayList<Book> books3 = new ArrayList<>();
        books1.add(new Book(1L,"晨岛快速跑图攻略1","哲学",88,"123123"));
        books1.add(new Book(2L,"晨岛快速跑图攻略2","哲学",88,"123123"));
        books1.add(new Book(3L,"晨岛快速跑图攻略2","哲学",88,"123123"));
        books2.add(new Book(1L,"晨岛快速跑图攻略1","哲学",88,"123123"));
        books2.add(new Book(2L,"晨岛快速跑图攻略2","哲学",88,"123123"));
        books2.add(new Book(3L,"晨岛快速跑图攻略2","哲学",88,"123123"));
        books3.add(new Book(7L,"晨岛快速跑图攻略3","哲学",88,"123123"));
        books3.add(new Book(8L,"晨岛快速跑图攻略3","哲学",88,"123123"));
        books3.add(new Book(9L,"晨岛快速跑图攻略3","哲学",88,"123123"));
        author1.setBooks(books1);
        author2.setBooks(books2);
        author3.setBooks(books3);
        author4.setBooks(books3);
        ArrayList<Author> authorList = new ArrayList<>(Arrays.asList(author1,author2,author3,author4));
        return authorList;
    }
}

image.gif

入门测试

打印年龄小于18的作家名字,并且去重

public class StreamDemo {
    public static void main(String[] args) {
        // 打印年龄小于18的作家名字,并且去重
        test1();
    }
    /**
     * 打印年龄小于18的作家名字,并且去重
     */
    private static void test1() {
        // 数据准备
        List<Author> authors = getAuthors();
        // 打印年龄小于18的作家名字,并且去重
        authors.stream()    // 把集合转换成流
                // 去重
                .distinct()
                // 筛选
                .filter(author -> author.getAge() < 18)
                // 打印
                .forEach(author -> System.out.println(author.getName()));
    }
}

image.gif

单列集合创建流(重要)

public class StreamDemo {
    public static void main(String[] args) {
        // 单列集合创建流(重要)
        test2();
    }
    /**
     * 单列集合创建流
     */
    private static void test2() {
        // 数据准备
        List<Author> authors = getAuthors();
        // 单列集合创建流
        Stream<Author> stream = authors.stream();
    }
}

image.gif

数组创建流

public class StreamDemo {
    public static void main(String[] args) {
        // 数组创建流(方式1)
        test3();
        // 数组创建流(方式2)
        test4();
    }
    /**
     * 数组创建流(方式2)
     */
    private static void test4() {
        // 创建数组
        Integer[] numArr = {1,2,3,4,5};
        // 数组创建流
        Stream<Integer> stream = Stream.of(numArr);
        // 输出
        stream.filter(integer -> integer > 2)
                .forEach(integer -> System.out.println(integer));
    }
    /**
     * 数组创建流(方式1)
     */
    private static void test3() {
        // 创建数组
        Integer[] numArr = {1,2,3,4,5};
        // 数组创建流
        Stream<Integer> stream = Arrays.stream(numArr);
        // 输出
        stream.filter(integer -> integer % 2 == 0)
                .forEach(integer -> System.out.println(integer));
    }
}

image.gif

Map创建流

public class StreamDemo {
    public static void main(String[] args) {
        // Map创建流
        test5();
    }
    /**
     * Map创建流
     */
    private static void test5() {
        HashMap<String, Integer> map = new HashMap<>();
        map.put("开朗觉觉",16);
        map.put("开朗莽莽",17);
        map.put("酪小美",18);
        // 将Map集合转换为Set集合 (Map集合是不能直接转换为Stream流的,所以要先转成Set集合)
        Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
        // 创建Stream流
        Stream<Map.Entry<String, Integer>> stream = entrySet.stream();
        // 输出
        stream.filter(item -> item.getValue() == 18)
                .forEach(item -> System.out.println(item.getKey() + "," + item.getValue() + "岁"));
    }
}

image.gif

中间操作常用方法

filter() 根据条件进行过滤

public class StreamCenter {
    public static void main(String[] args) {
        // filter() 打印所有名字长度大于2的作家姓名
        test1();
    }
    /**
     * filter() 打印所有名字长度大于2的作家姓名
     */
    private static void test1() {
        // 数据准备
        List<Author> authors = getAuthors();
        // 获取流并打印
        authors.stream()
                .distinct()
                .filter(author -> author.getName().length() > 2)
                .forEach(author -> System.out.println(author.getName()));
    }
}

image.gif

map() 将流中的元素进行转换

public class StreamCenter {
    public static void main(String[] args) {
        // map() 将流中的元素进行转换
        test2();
    }
    /**
     * map() 将流中的元素进行转换
     */
    private static void test2() {
        // 数据准备
        List<Author> authors = getAuthors();
        // 创建流并输出
        authors.stream()
                .map(author -> author.getName())
                .forEach(name -> System.out.println(name));
    }
}

image.gif

map() 将流中元素进行计算

public class StreamCenter {
    public static void main(String[] args) {
        // map() 将流中元素进行计算
        test3();
    }
    /**
     * map() 将流中元素进行计算
     */
    private static void test3() {
        // 数据准备
        List<Author> authors = getAuthors();
        // 创建流并输出
        authors.stream()
                .map(author -> author.getAge())
                .map(age -> age + 10)
                .forEach(System.out::println);
    }
}

image.gif

distinct() 去除重复元素

注意:distinct方法是依赖Object的equals方法来判断是否是相同对象的。所以注意重写equals方法;

public class StreamCenter {
    public static void main(String[] args) {
        // distinct() 去除重复元素 (distinct方法是依赖Object的equals方法来判断是否是相同对象的。所以注意重写equals方法)
        test4();
    }
    /**
     * distinct() 去除重复元素 (distinct方法是依赖Object的equals方法来判断是否是相同对象的。所以注意重写equals方法)
     */
    private static void test4() {
        // 数据准备
        List<Author> authors = getAuthors();
        // 创建流并输出
        authors.stream()
                .distinct()
                .forEach(author -> System.out.println(author.getName()));
    }
}

image.gif

sorted() 对流中的元素进行排序

注意:如果调用空参的sorted方法,需要流中的元素是实现了Comparable;

public class StreamCenter {
    public static void main(String[] args) {
        // sorted() 对流中的元素按年龄进行排序 (注意如果调用空参的sorted方法,需要流中的元素是实现了Comparable)
        test5();
    }
    /**
     * sorted() 对流中的元素按年龄进行排序
     */
    private static void test5() {
        // 数据准备
        List<Author> authors = getAuthors();
        // 创建流并输出
        authors.stream()
                .sorted((o1, o2) -> o1.getAge() - o2.getAge())
                .forEach(author -> System.out.println(author.getName() + "," + author.getAge()));
    }
}

image.gif

limit() 设置流的最大长度

注意:超出的部将被抛弃;

public class StreamCenter {
    public static void main(String[] args) {
        // limit() 对流中的元素按照年龄进行降序排序,并且要求不能有重复的元素,然后打印其中两个年龄最大的作家
        test6();
    }
    /**
     * limit() 对流中的元素按照年龄进行降序排序,并且要求不能有重复的元素,然后打印其中两个年龄最大的作家
     */
    private static void test6() {
        // 数据准备
        List<Author> authors = getAuthors();
        // 创建流并输出
        authors.stream()
                .distinct()
                .sorted((o1, o2) -> o2.getAge() - o1.getAge())
                .limit(2)
                .forEach(author -> System.out.println(author.getName() + "," + author.getAge()));
    }
}

image.gif

skip() 跳过流中的几个元素

public class StreamCenter {
    public static void main(String[] args) {
        // skip() 跳过流中的几个元素
        test7();
    }
    // skip() 跳过流中的几个元素
    private static void test7() {
        // 数据准备
        List<Author> authors = getAuthors();
        // 创建流并输出
        authors.stream()
                .skip(1)
                .forEach(author -> System.out.println(author.getName()));
    }
}

image.gif

flatMap() 将流中元素的多个对象作为流中的元素

public class StreamCenter {
    public static void main(String[] args) {
        // flatMap() 将流中元素的多个对象作为流中的元素
        test8();
    }
    /**
     * flatMap() 将流中元素的多个对象作为流中的元素
     */
    private static void test8() {
        // 数据准备
        List<Author> authors = getAuthors();
        // 创建流并输出
        authors.stream()
                .flatMap(author -> author.getBooks().stream())
                .distinct()
                .forEach(book -> System.out.println(book.getName()));
    }
}

image.gif

打印现有数据的所有分类,要求去重,不能出现连着的分类:哲学,爱情

public class StreamCenter {
    public static void main(String[] args) {
        // 打印现有数据的所有分类,要求去重,不能出现连着的分类:哲学,爱情
        test9();
    }
    /**
     * 打印现有数据的所有分类,要求去重,不能出现连着的分类:哲学,爱情
     */
    private static void test9() {
        // 数据准备
        List<Author> authors = getAuthors();
        // 创建流并输出
        authors.stream()
                .flatMap(author -> author.getBooks().stream())
                .distinct()
                .flatMap(book -> Arrays.stream(book.getCategory().split(",")))
                .distinct()
                .forEach(category -> System.out.println(category));
    }
}

image.gif

终结操作

forEach() 对流中的元素进行遍历操作

对流中的元素进行遍历操作,通过传入传入的参数去指定对遍历的元素进行什么具体操作;

private static void test01(List<Author> authors) {
    // 打印所有作家的名字,注意删除重复元素
    authors.stream()
            // 将流中的元素进行转换,并返回
            .map(author -> author.getName())
            // 去重
            .distinct()
            // 遍历流中的元素
            .forEach(System.out::println);
}

image.gif

count() 获取当前流中元素的个数

private static void test02(List<Author> authors) {
    // 打印作家所出的书籍的数目,注意删除重复元素
    long count = authors.stream()
            .flatMap(author -> author.getBooks().stream())
            .distinct()
            .count();
    // 输出
    System.out.println(count);
}

image.gif

max()&min() 获取流中最大值-最小值

private static void test03(List<Author> authors) {
    // 获取这些作家分数最高和最低的书籍
    Optional<Integer> max = authors.stream()
            .flatMap(author -> author.getBooks().stream())
            .map(book -> book.getScore())
            .max((score1, score2) -> score1 - score2);
    System.out.println("最大值:" + max.get());
    Optional<Integer> min = authors.stream()
            .flatMap(author -> author.getBooks().stream())
            .map(book -> book.getScore())
            .min((score1, score2) -> score1 - score2);
    System.out.println("最小值:" + min.get());
}

image.gif

collect() 把当前流转换成一个集合(重点)

List集合

private static void test04(List<Author> authors) {
    // 获取一个存放所有作者名字的List集合
    List<String> nameList = authors.stream()
            .map(author -> author.getName())
            .collect(Collectors.toList());
    System.out.println(nameList);
}

image.gif

set集合

private static void test05(List<Author> authors) {
    // 获取一个所有书名的Set集合
    Set<String> bookNameSet = authors.stream()
            .flatMap(author -> author.getBooks().stream())
            .map(book -> book.getName())
            .collect(Collectors.toSet());
    System.out.println(bookNameSet);
}

image.gif

map集合

注意map集合的key值不能重复

private static void test06(List<Author> authors) {
    // 获取一个map集合,key是作者名,value是书名的list集合
    Map<String, List<Book>> nameAndBookNameMap = authors.stream()
            .distinct()
            .collect(Collectors.toMap(author -> author.getName(), author -> author.getBooks()));
}

image.gif

anyMatch() 判断是否有任意符合匹配条件的元素,结果为boolean类型

anyMatch() 和filter()的区别:

filter():根据条件过滤流中的元素,符合条件的继续往下走

anyMatch():根据条件判断,是否有符合条件的元素,有则返回true,没有返回false

private static void test07(List<Author> authors) {
    // 判断是否有年龄在29以上的作家
    boolean okAndNo = authors.stream()
            .anyMatch(author -> author.getAge() > 29);
    System.out.println(okAndNo);
}

image.gif

allMatch() 判断是否全部都符合条件,结果为boolean类型

private static void test08(List<Author> authors) {
    // 判断作家是否都是成年人
    boolean okAndNo = authors.stream()
            .allMatch(author -> author.getAge() >= 18);
    System.out.println(okAndNo);
}

image.gif

noneMatch() 判断流中的元素是否都不符合匹配条件,如果都不符合结果为true,否则为false

private static void test09(List<Author> authors) {
    // 判断作家是否都没超过100岁
    boolean okAndNo = authors.stream()
            .noneMatch(author -> author.getAge() > 100);
    System.out.println(okAndNo);
}

image.gif

findAny() 取出流中的任意一个元素

private static void test10(List<Author> authors) {
    // 获取任意一个年龄大于18的作家,如果存在就输出名字
    Optional<Author> authorOptional = authors.stream()
            .filter(author -> author.getAge() > 18)
            .findAny();
    authorOptional.ifPresent(author -> System.out.println(author.getName()));
}

image.gif

findFirst() 获取流中第一个元素

private static void test11(List<Author> authors) {
    // 获取一个年龄最小的作家,并输出他的名字
    Optional<Author> minAge = authors.stream()
            .sorted((o1, o2) -> o1.getAge() - o2.getAge())
            .findFirst();
    minAge.ifPresent(author -> System.out.println(author.getName()));
}

image.gif

reduce() 对流中的数据按照你指定的计算方式计算出一个结果

reduce两个参数的重载形式内部的计算方式如下:

T result = identity;
for (T element : this stream) {
    result = accumulator.apply(result,element)
}
return result;

image.gif

求所有作家的年龄和

public class StreamEnd {
    public static void main(String[] args) {
        // reduce() 对流中的数据按照你指定的计算方式计算出一个结果
        // 求所有作家的年龄和
//        test12(authors);
    }
    /**
     * reduce() 对流中的数据按照你指定的计算方式计算出一个结果
     * @param authors
     */
    private static void test12(List<Author> authors) {
        // 求所有作家的年龄和
        Integer avgSum = authors.stream()
                .map(author -> author.getAge())
                .reduce(0, (result, element) -> result + element);
        System.out.println(avgSum);
    }
}

image.gif

求所有作家中年龄最大的值

public class StreamEnd {
    public static void main(String[] args) {
        // 求所有作家中年龄最大的值
        test13(authors);
    }
    /**
     * reduce() 对流中的数据按照你指定的计算方式计算出一个结果
     * @param authors
     */
    private static void test13(List<Author> authors) {
        // 求所有作家中年龄最大的值
        Integer ageMax = authors.stream()
                .map(author -> author.getAge())
                .reduce(Integer.MIN_VALUE, (result, element) -> result < element ? element : result);
        System.out.println(ageMax);
    }
}

image.gif

求作家中年龄的最小值

public class StreamEnd {
    public static void main(String[] args) {
        // 求所有作家中年龄最大的值
    test14(authors);
    }
    /**
     * reduce() 对流中的数据按照你指定的计算方式计算出一个结果
     * @param authors
     */
    private static void test14(List<Author> authors) {
        // 求作家中年龄的最小值
        Integer ageMin = authors.stream()
                .map(author -> author.getAge())
                .reduce(Integer.MAX_VALUE, (result, element) -> result < element ? result : element);
        System.out.println(ageMin);
    }
}

image.gif

reduce一个参数的重载形式内部的计算

这个就相当于是把第一个元素值当成默认值来用(要注意,不要为为出现空值)

boolean foundAny = false;
T result = null;
for (T element : this stream) {
    if (!foundAny) {
        foundAny = true;
        result = element;
    } else {
        result = accumulator.apply(result,element);
    }
}
return foundAny ? Optional.of(result) : Optional.empty();

image.gif

用一个参数的reduce求作家中年龄的最小值

public class StreamEnd {
    public static void main(String[] args) {
        // 用一个参数的reduce求作家中年龄的最小值
        test15(authors);
    }
    /**
     * reduce() 对流中的数据按照你指定的计算方式计算出一个结果
     * @param authors
     */
    private static void test15(List<Author> authors) {
        // 用一个参数的reduce求作家中年龄的最小值
        Optional<Integer> authorsOptional = authors.stream()
                .map(author -> author.getAge())
                .reduce((result, element) -> result > element ? element : result);
        authorsOptional.ifPresent(age -> System.out.println(age));
    }
}

image.gif

注意事项

1、惰性求值(如果没有终结操作,没有中间操作时不会得到执行的)

2、流是一次性的(一旦一个流对象经过一个终结操作后。这个流就不能再被使用)

3、不会影响原数据(我们在流中可以多数据做很多处理。但是正常情况下,不会影响原来集合中的元素的。这往往也是我们期待的)

以下是非正常操作(会影响原数据)

authors.stream()
    .map(author -> {
        author.setAge(author.getAge() + 10);
        return author;
    })
    .forEach(list -> System.out.println(list));
System.out.println(authors);

image.gif


相关文章
|
6天前
|
JSON API 数据格式
OpenCode入门使用教程
本教程介绍如何通过安装OpenCode并配置Canopy Wave API来使用开源模型。首先全局安装OpenCode,然后设置API密钥并创建配置文件,最后在控制台中连接模型并开始交互。
2481 6
|
12天前
|
人工智能 JavaScript Linux
【Claude Code 全攻略】终端AI编程助手从入门到进阶(2026最新版)
Claude Code是Anthropic推出的终端原生AI编程助手,支持40+语言、200k超长上下文,无需切换IDE即可实现代码生成、调试、项目导航与自动化任务。本文详解其安装配置、四大核心功能及进阶技巧,助你全面提升开发效率,搭配GitHub Copilot使用更佳。
|
14天前
|
存储 人工智能 自然语言处理
OpenSpec技术规范+实例应用
OpenSpec 是面向 AI 智能体的轻量级规范驱动开发框架,通过“提案-审查-实施-归档”工作流,解决 AI 编程中的需求偏移与不可预测性问题。它以机器可读的规范为“单一真相源”,将模糊提示转化为可落地的工程实践,助力开发者高效构建稳定、可审计的生产级系统,实现从“凭感觉聊天”到“按规范开发”的跃迁。
2043 18
|
2天前
|
人工智能 API 开发者
Claude Code 国内保姆级使用指南:实测 GLM-4.7 与 Claude Opus 4.5 全方案解
Claude Code是Anthropic推出的编程AI代理工具。2026年国内开发者可通过配置`ANTHROPIC_BASE_URL`实现本地化接入:①极速平替——用Qwen Code v0.5.0或GLM-4.7,毫秒响应,适合日常编码;②满血原版——经灵芽API中转调用Claude Opus 4.5,胜任复杂架构与深度推理。
|
6天前
|
人工智能 前端开发 Docker
Huobao Drama 开源短剧生成平台:从剧本到视频
Huobao Drama 是一个基于 Go + Vue3 的开源 AI 短剧自动化生成平台,支持剧本解析、角色与分镜生成、图生视频及剪辑合成,覆盖短剧生产全链路。内置角色管理、分镜设计、视频合成、任务追踪等功能,支持本地部署与多模型接入(如 OpenAI、Ollama、火山等),搭配 FFmpeg 实现高效视频处理,适用于短剧工作流验证与自建 AI 创作后台。
951 4
|
12天前
|
人工智能 JavaScript 前端开发
【2026最新最全】一篇文章带你学会Cursor编程工具
本文介绍了Cursor的下载安装、账号注册、汉化设置、核心模式(Agent、Plan、Debug、Ask)及高阶功能,如@引用、@Doc文档库、@Browser自动化和Rules规则配置,助力开发者高效使用AI编程工具。
1513 7
|
5天前
|
人工智能 运维 前端开发
Claude Code 30k+ star官方插件,小白也能写专业级代码
Superpowers是Claude Code官方插件,由核心开发者Jesse打造,上线3个月获3万star。它集成brainstorming、TDD、系统化调试等专业开发流程,让AI写代码更规范高效。开源免费,安装简单,实测显著提升开发质量与效率,值得开发者尝试。
|
16天前
|
人工智能 测试技术 开发者
AI Coding后端开发实战:解锁AI辅助编程新范式
本文系统阐述了AI时代开发者如何高效协作AI Coding工具,强调破除认知误区、构建个人上下文管理体系,并精准判断AI输出质量。通过实战流程与案例,助力开发者实现从编码到架构思维的跃迁,成为人机协同的“超级开发者”。
1172 99
|
12天前
|
人工智能 JSON 自然语言处理
【2026最新最全】一篇文章带你学会Qoder编辑器
Qoder是一款面向程序员的AI编程助手,集智能补全、对话式编程、项目级理解、任务模式与规则驱动于一体,支持模型分级选择与CLI命令行操作,可自动生成文档、优化提示词,提升开发效率。
913 10
【2026最新最全】一篇文章带你学会Qoder编辑器

热门文章

最新文章