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 + ")";
    }
}

数据结构:

运行结果:

相关推荐
小王不爱笑1322 小时前
Kubernetes(K8s)核心知识点
java
桑榆肖物2 小时前
.NET 10 Native AOT 在 Linux 嵌入式设备上的实战
java·linux·.net·aot
墨着染霜华2 小时前
Java实战:封装Redis非阻塞分布式锁,彻底解决表单重复提交主键冲突
java·redis·分布式
启山智软2 小时前
【使用 Java(JSP)实现的简单商城页面前端示例】
java·前端·商城开发
一个有温度的技术博主2 小时前
Redis系列七:Java客户端Jedis的入门
java·数据库·redis
LSL666_2 小时前
BaseMapper——新增和删除
java·开发语言·mybatis·mybatisplus
后端AI实验室2 小时前
我让AI模拟面试官考了我一个小时,然后我沉默了
java·ai
金銀銅鐵2 小时前
Byte Buddy 生成的类的结构如何?(第二篇)
java·后端
StackNoOverflow2 小时前
Spring MVC零散知识点记录
java·spring·mvc