Java stream 流的基本使用

Java stream 的基本使用

java 复制代码
package com.zhong.streamdemo.usestreamdemo;

import jdk.jfr.DataAmount;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;

/**
 * @ClassName : UseStream
 * @Description : stream 流的简单使用
 * @Author : zhx
 * @Date: 2024-02-08 15:24
 */
public class UseStream {
    public static void main(String[] args) {
        // filter 过滤不满足条件的元素 返回满足条件的元素
        System.out.println("-------------筛选成绩大于60分的信息-------------");
        ArrayList<Double> strem1 = new ArrayList<>(List.of(60.1, 45.2, 90.2, 99.9, 76.3));
        List<Double> collect = strem1.stream()
                .filter(x -> x > 60)
                .toList();
        System.out.println(collect);

        // stream 对于对象的操作
        System.out.println("-------------stream 对于对象的操作-------------");
        ArrayList<Student> students = new ArrayList<>(List.of(
                new Student("小钟", 22, 179.1),
                new Student("小钟", 22, 179.1),
                new Student("小王", 21, 153.9),
                new Student("小王", 21, 153.9),
                new Student("张三", 52, 160.8),
                new Student("李四", 42, 140.5),
                new Student("王五", 18, 135.3)
        ));
        // 筛选年龄大于 17 且小于 30 的学生信息
        System.out.println("-------------筛选年龄大于 17 且小于 30 的学生信息-------------");
        List<Student> list = students.stream()
                .filter(x -> x.getAge() > 17)       // 过滤年龄小于等于 17 的
                .filter(x -> x.getAge() < 30)       // 过滤年龄大于等于 30 的
                .toList();
        list.forEach(System.out::println);

        // 筛选身高最高的 3 名学生信息
        System.out.println("-------------筛选身高前三的学生信息-------------");
        List<Student> list1 = students.stream()
                .sorted(Comparator.comparing(Student::getHeight))   // 按照身高排序
                .skip(students.size() - 3)                      // skip(n) 忽略数组 -3 长度 相当于截取最后三个元素
                .toList();
        list1.forEach(System.out::println);

        // 筛选身高最后的 2 名学生信息
        System.out.println("-------------筛选身高前三的学生信息-------------");
        List<Student> list2 = students.stream()
                .sorted(Comparator.comparing(Student::getHeight))
                .limit(2)       // 截取几个元素
                .toList();
        list2.forEach(System.out::println);

        // 筛选身高超过 153 的学生的姓名 去除重复的名字
        System.out.println("-------------筛选身高超过 153 的学生的姓名 去除重复的名字-------------");
        List<String> list3 = students.stream()
                .filter(x -> x.getHeight() > 153)
                .map(Student::getName)  // 转换算子
                .distinct()             // distinct() 去重
                .toList();

        // distinct() 去重 如果是对象去重 需要重写 hashCode 和 equals 方法
        System.out.println(list3);

        // Stream.concat(stream1, stream2) 合并流
        System.out.println("-------------Stream.concat(stream1, stream2) 合并流-------------");
        Stream<String> stream1 = Stream.of("张三1", "李四1");
        Stream<String> stream2 = Stream.of("张三2", "李四2", "王五2");
        Stream<String> concatStream = Stream.concat(stream1, stream2);
        concatStream.forEach(System.out::println);
    }
}

@Data
@AllArgsConstructor
@NoArgsConstructor
class Student {
    private String name;
    private int age;
    private double height;

    // distinct() 去重 如果是对象去重 需要重写 hashCode 和 equals 方法
//    @Override
//    public boolean equals(Object o) {
//        if (this == o) return true;
//        if (o == null || getClass() != o.getClass()) return false;
//        Student student = (Student) o;
//        return age == student.age && Double.compare(height, student.height) == 0 && Objects.equals(name, student.name);
//    }
//
//    @Override
//    public int hashCode() {
//        return Objects.hash(name, age, height);
//    }
}
相关推荐
克拉克盖博16 分钟前
chapter03_Bean的实例化与策略模式
java·spring·策略模式
赞哥哥s16 分钟前
Python脚本开发-统计Rte中未连接的Port
python·autosar·rte
Franklin18 分钟前
Python界面设计【QT-creator基础编程 - 01】如何让不同分辨率图像自动匹配graphicsView的窗口大小
开发语言·python·qt
waynaqua24 分钟前
FastAPI开发AI应用三:添加深度思考功能
python·openai·deepseek
DashVector26 分钟前
如何通过Java SDK分组检索Doc
java·数据库·面试
onejason27 分钟前
《利用 Python 爬虫获取 Amazon 商品详情实战指南》
前端·后端·python
LZQqqqqo42 分钟前
C# 中 ArrayList动态数组、List<T>列表与 Dictionary<T Key, T Value>字典的深度对比
windows·c#·list
季春二九44 分钟前
Windows 11 首次开机引导(OOBE 阶段)跳过登录微软账户,创建本地账户
windows·microsoft
程序员清风1 小时前
跳表的原理和时间复杂度,为什么还需要字典结构配合?
java·后端·面试
渣哥1 小时前
Kafka消息丢失的3种场景,生产环境千万要注意
java