Stream流的常用方法(自用)

自用的笔记, 有🚩 需要多看

基本数据

自定义实体

java 复制代码
@Data
class Student{
        private String name;

        private Integer age;

        private Double height;

        public Student() {
        }
}

假数据

java 复制代码
Student s1 = new Student();
s1.setAge(20);
s1.setName("cookie");
s1.setHeight(180d);

Student s2 = new Student();
s2.setAge(30);
s2.setName("cookie");
s2.setHeight(180d);

Student s3 = new Student();
s3.setAge(40);
s3.setName("bob");
s3.setHeight(175d);

Student s4 = new Student();
s4.setAge(40);
s4.setName("bob");
s4.setHeight(180d);

// 存入list集合
List<Student> list = new ArrayList<>();
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);

一, 分组

1. 一层分组/简单分组

java 复制代码
/**
 * 需求一(一层分组):根据Age分组
 */
System.out.println("需求一(一层分组):根据Age分组");
Map<Integer, List<Student>> collect = list.stream().collect(Collectors.groupingBy(Student::getAge));
for (Integer age : collect.keySet()) {
    System.out.println("key:" + age + "\tvalue:" + collect.get(age));
}

/**
 * 控制台结果:
 * key:20	value:[Student(name=cookie, age=20, height=180.0)]
 * key:40	value:[Student(name=bob, age=40, height=175.0), Student(name=bob, age=40, height=180.0)]
 * key:30	value:[Student(name=cookie, age=30, height=180.0)]
 */

2. 多层分组

java 复制代码
/**
 * 需求二: 先根据name分组,然后再根据身高分组
 */
System.out.println("需求二: 先根据name分组,然后再根据身高分组");
Map<String, Map<Double, List<Student>>> collect1 = list.stream()
        .collect(Collectors.groupingBy(Student::getName, Collectors.groupingBy(Student::getHeight)));
Set<String> namesGroup = collect1.keySet();
for (String namekey : namesGroup) {
    Map<Double, List<Student>> heightGroupMap = collect1.get(namekey);
    Set<Double> height = heightGroupMap.keySet();
    for (Double h : height) {
        System.out.println("name:" + namekey + " height:" + heightGroupMap.get(h));
    }
}

/**
 * 控制台结果:
 * name:bob height:[Student(name=bob, age=40, height=175.0)]
 * name:bob height:[Student(name=bob, age=40, height=180.0)]
 * name:cookie height:[Student(name=cookie, age=20, height=180.0), Student(name=cookie, age=30, height=180.0)]
 */

3. 多层分组-自定义key 🚩

java 复制代码
/**
 * 需求三: 自定义key返回 形式如下: age_height bob_175
 */
System.out.println("需求三: 自定义key返回 形式如下: age_height bob_175");
Map<String, List<Student>> collect2 = list.stream()
    .collect(Collectors.groupingBy(c -> c.getName() + "_" + c.getHeight()));

for (String customKey : collect2.keySet()) {
    System.out.println("key:" + customKey +" value:"+ collect2.get(customKey));
}
/**
 * 控制台结果:
 * key:bob_180.0 value:[Student(name=bob, age=40, height=180.0)]
 * key:bob_175.0 value:[Student(name=bob, age=40, height=175.0)]
 * key:cookie_180.0 value:[Student(name=cookie, age=20, height=180.0), Student(name=cookie, age=30, height=180.0)]
 */

二, 排序

方式一: 通过自定义的比较器(非必要不推荐)

java 复制代码
/**
* 需求: 根据身高排序,如果身高相同,根据年龄排序,如果年龄依然相同,根据名称字母顺序排序
*/
List<Student> collect3 = list.stream().sorted(new Comparator<Student>() {
    @Override
    public int compare(Student o1, Student o2) {
        // 这里前面的减去后面的是升序, 反之这是降序
        if (!o1.getHeight().equals(o2.getHeight())) {
            return (int) (o1.getHeight() - o2.getHeight());
        }
        if (!o1.getAge().equals(o2.getAge())) {
            return o1.getAge() - o2.getAge();
        }
        return o1.getName().compareTo(o2.getName());
    }
}).collect(Collectors.toList());
System.out.println(collect3);

/**
 * 控制台结果:
 * [Student(name=bob, age=40, height=175.0), 
 * Student(name=cookie, age=20, height=180.0), 
 * Student(name=cookie, age=30, height=180.0), 
 * Student(name=bob, age=40, height=180.0)]
 */

// 注: 当然上面的也可以做一个简化
List<Student> collect3 = list.stream().sorted((o1, o2) -> {
    // 这里前面的减去后面的是升序, 反之这是降序
    if (!o1.getHeight().equals(o2.getHeight())) {
        return (int) (o1.getHeight() - o2.getHeight());
    }
    if (!o1.getAge().equals(o2.getAge())) {
        return o1.getAge() - o2.getAge();
    }
    return o1.getName().compareTo(o2.getName());
}).collect(Collectors.toList());

方式二: 通过lambda 🚩

java 复制代码
List<Student> collect4 = list.stream()
	.sorted(Comparator.comparingDouble(Student::getHeight)
        .thenComparingInt(Student::getAge)
        .thenComparing(Student::getName))
  	.collect(Collectors.toList());
System.out.println(collect4);

/**
 * 控制台结果:
 * [Student(name=bob, age=40, height=175.0), 
 * Student(name=cookie, age=20, height=180.0), 
 * Student(name=cookie, age=30, height=180.0), 
 * Student(name=bob, age=40, height=180.0)]
 */

// 注意:
// 方式一,升序降序是通过返回的正负, 
// 方式二而是通过方法, 现在我们首先通过身高降序, 我们只需要在条件的后面加一个reversed()后缀方法即可

List<Student> collect4 = list.stream().sorted(Comparator.comparingDouble(Student::getHeight).reversed()
        .thenComparingInt(Student::getAge)
        .thenComparing(Student::getName)
).collect(Collectors.toList());
System.out.println(collect4);

/**
 * 修改之后控制台结果:
 * [Student(name=cookie, age=20, height=180.0), 
 * Student(name=cookie, age=30, height=180.0), 
 * Student(name=bob, age=40, height=180.0), 
 * Student(name=bob, age=40, height=175.0)]
 */

三, 统计

Java 复制代码
/**
 * 需求: 统计年龄之和
 */
int ageSum = list.stream().mapToInt(Student::getAge).sum();


/**
 * 求年龄平均值
 */
Double ageAvg1 = list.stream().collect(Collectors.averagingInt(Student::getAge));
// 或者
double ageAvg2 = list.stream().mapToInt(Student::getAge).average().getAsDouble();

/**
 * 求年龄最大值
 */
int maxAge = list.stream().mapToInt(Student::getAge).max().getAsInt();

/**
 * 最小值
 */
int minAge = list.stream().mapToInt(Student::getAge).min().getAsInt();

缓慢总结中~~~~

相关推荐
挖掘狂人3 分钟前
从底层到优化:深入理解 synchronized 的 “锁” 事
java·程序员·cpu
郝学胜-神的一滴11 小时前
Effective STL 条款15:注意字符串实现上的差异
java·开发语言·数据结构·c++·算法·visualstudio
sharemylove11 小时前
Java学习笔记 —— 面向对象专题
java·开发语言·笔记·学习·课程设计
代码民工正电荷12 小时前
JAVA学习基础(一)
java·开发语言·学习
两点王爷13 小时前
Java 高频面试题详解:项目中常用的注解
java·开发语言
持敬chijing16 小时前
JAVA与CVE漏洞4-Log4J 漏洞原理与实现
java·网络安全·log4j·系统安全·网络攻击模型·安全威胁分析
软件技术NINI16 小时前
基于SpringBoot的社区留守儿童帮扶系统
java·spring boot·后端
会编程的吕洞宾18 小时前
Spring Boot虚拟线程实战:吞吐提升3倍
java·经验分享·后端·spring
Rain的Java大神之路18 小时前
Spring Boot 4 + Spring AI 2.0 迁移实战:别急着改版本号,先算算你欠了多少债
java·后端·ai编程
代码不停19 小时前
Linux基础知识
java·linux·数据库