Java List 根据List中对象的属性值是否相同作为同一组,分割成多个连续的子List

需求:Java List 根据List中对象的属性值是否相同作为同一组,分割成多个连续的子List
java 复制代码
package com.suncd.trs.provider.controller;


import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

public class ListGrouping {

    /**
     * 将List按照对象属性值是否相同进行分组,分割成多个连续的子List
     * @param list 原始List
     * @param keyExtractor 提取对象属性值的函数
     * @param <T> List中对象的类型
     * @param <K> 属性值的类型
     * @return 分割后的子List集合
     */
    public static <T, K> List<List<T>> groupByProperty(List<T> list, Function<T, K> keyExtractor) {
        if (list == null || keyExtractor == null) {
            throw new IllegalArgumentException("List和keyExtractor不能为null");
        }

        List<List<T>> result = new ArrayList<>();
        if (list.isEmpty()) {
            return result;
        }

        List<T> currentGroup = new ArrayList<>();
        K previousKey = null;

        for (T item : list) {
            K currentKey = keyExtractor.apply(item);

            // 如果是第一个元素,或者当前属性值与前一个不同,则开始新组
            if (currentGroup.isEmpty() || !currentKey.equals(previousKey)) {
                if (!currentGroup.isEmpty()) {
                    result.add(currentGroup);
                }
                currentGroup = new ArrayList<>();
            }

            currentGroup.add(item);
            previousKey = currentKey;
        }

        // 添加最后一组
        if (!currentGroup.isEmpty()) {
            result.add(currentGroup);
        }

        return result;
    }

    // 示例使用
    public static void main(String[] args) {
        // 创建测试数据
        List<Person> people = new ArrayList<>();
        people.add(new Person("张三", 25));
        people.add(new Person("李四", 25));
        people.add(new Person("王五", 30));
        people.add(new Person("赵六", 27));
        people.add(new Person("钱七", 30));
        people.add(new Person("孙八", 25));
        people.add(new Person("周九", 35));

        // 按年龄分组
        List<List<Person>> groupedByAge = groupByProperty(people, Person::getAge);
        System.out.println("按年龄分组:");
        for (int i = 0; i < groupedByAge.size(); i++) {
            System.out.println("组 " + (i + 1) + ": " + groupedByAge.get(i));
        }
    }
}

class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    @Override
    public String toString() {
        return name + "(" + age + ")";
    }
}

数据结构:

运行结果:

相关推荐
NE_STOP11 小时前
Vide Coding--AI编程工具的选择
java
码云数智-园园11 小时前
C++20 Modules 模块详解
java·开发语言·spring
程序员黑豆11 小时前
JDK 下载安装与配置详细教程
java·前端·ai编程
小宇宙Zz12 小时前
Maven依赖冲突
java·服务器·maven
swordbob12 小时前
NIO的channel中什么是 fd(File Descriptor,文件描述符)
java·开发语言·nio
咖啡八杯12 小时前
GoF设计模式——享元模式
java·spring·设计模式·享元模式
小小工匠12 小时前
Redis - 事务机制:能实现 ACID 属性吗
数据结构·redis·性能优化·并发·持久化
十五喵源码网12 小时前
基于springboot2+vue2的租房管理系统
java·毕业设计·springboot·论文笔记
摇滚侠12 小时前
IDEA 创建 Java 项目 手动整合 SSM 框架
java·ide·intellij-idea
源分享12 小时前
Java线程同步的多种实现方法(非常详细)
java·开发语言·jvm