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

数据结构:

运行结果:

相关推荐
动词ing29 分钟前
【C语言】结构体+文件基础
c语言·开发语言·数据结构
亚历克斯神1 小时前
智能搜索系统的升级复盘——从 Elasticsearch 到混合检索的检索质量提升
java·spring·微服务
金銀銅鐵2 小时前
[Java] 一个方法最多可以有多少个入参?
java·jvm
哭哭啼2 小时前
JAVA服务问题诊断
java·开发语言·jvm
Sayuanni%32 小时前
SpringBoot 从注解到源码:核心知识点总结
java·spring boot·后端
坚定信念,勇往无前3 小时前
Maven 私有仓库-nexus
java
Minner-Scrapy3 小时前
Scrapy 2.17 源码解析:Scheduler 调度器与磁盘/内存双队列
java·爬虫·python·scrapy·网络爬虫·twisted
晚风醉蝶3 小时前
1-11-奇偶排序-OddEvenSort
java·数据结构·算法
旖旎夜光3 小时前
LeetCode 852:山脉数组的峰顶索引(二分查找) —— 题解
数据结构·c++·算法·leetcode·二分查找
夏天拐跑了西瓜4 小时前
Spring Cloud 微服务实战(三):一个服务挂了,凭什么拖垮整个系统?Sentinel 限流熔断实战
java·spring cloud·微服务