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);
//    }
}
相关推荐
阿_旭1 天前
基于YOLO26深度学习的【辣椒成熟度检测与计数系统】【python源码+Pyqt5界面+数据集+训练代码】
人工智能·python·深度学习·辣椒成熟度检测
无风听海1 天前
Python类型守卫深度解析
python
那个失眠的夜1 天前
AspectJ
java·开发语言·数据库·spring
杨凯凡1 天前
【014】基本类型与包装类:缓存、相等性、NPE
java·数据结构·缓存
weixin_580614001 天前
如何防止SQL注入利用存储过程_确保存储过程不拼字符串.txt
jvm·数据库·python
emmjng3691 天前
使用飞算JavaAI实现在线图书借阅平台
java
CoderYanger1 天前
14届蓝桥杯省赛Java A 组Q1~Q3
java·开发语言·线性代数·算法·职场和发展·蓝桥杯
钮钴禄·爱因斯晨1 天前
他到底喜欢我吗?赛博塔罗Java+前端实现,一键解答!
java·开发语言·前端·javascript·css·html
词元Max1 天前
Java 转 AI Agent 开发学习路线(2026年3月最新版)
java·人工智能·学习
亚历克斯神1 天前
Java 云原生开发最佳实践:构建现代化应用
java·spring·微服务