java list怎么进行group

在Java中,对List进行分组有多种方法,以下是常见的几种方式:

1. 使用 Stream API (Java 8+ 推荐)

1.1 按属性分组

复制代码
// 按对象属性分组
List<Person> people = Arrays.asList(
    new Person("Alice", "IT"),
    new Person("Bob", "HR"),
    new Person("Charlie", "IT")
);

Map<String, List<Person>> byDept = people.stream()
    .collect(Collectors.groupingBy(Person::getDepartment));

// 复杂分组:按多个条件分组
Map<String, Map<Integer, List<Person>>> byDeptAndAge = people.stream()
    .collect(Collectors.groupingBy(
        Person::getDepartment,
        Collectors.groupingBy(Person::getAge)
    ));

1.2 分组后处理

复制代码
// 分组并计数
Map<String, Long> countByDept = people.stream()
    .collect(Collectors.groupingBy(
        Person::getDepartment,
        Collectors.counting()
    ));

// 分组并求和
Map<String, Integer> sumByDept = people.stream()
    .collect(Collectors.groupingBy(
        Person::getDepartment,
        Collectors.summingInt(Person::getSalary)
    ));

// 分组后获取最大/最小值
Map<String, Optional<Person>> maxByDept = people.stream()
    .collect(Collectors.groupingBy(
        Person::getDepartment,
        Collectors.maxBy(Comparator.comparing(Person::getSalary))
    ));

2. 使用传统循环

复制代码
Map<String, List<Person>> byDept = new HashMap<>();

for (Person person : people) {
    String dept = person.getDepartment();
    byDept.computeIfAbsent(dept, k -> new ArrayList<>())
          .add(person);
}

3. 使用第三方库

3.1 Apache Commons Collections

复制代码
// 添加依赖
// implementation 'org.apache.commons:commons-collections4:4.4'

MultiValuedMap<String, Person> byDept = new ArrayListValuedHashMap<>();
for (Person person : people) {
    byDept.put(person.getDepartment(), person);
}

3.2 Google Guava

复制代码
// 添加依赖
// implementation 'com.google.guava:guava:31.1-jre'

Multimap<String, Person> byDept = ArrayListMultimap.create();
for (Person person : people) {
    byDept.put(person.getDepartment(), person);
}

// 或者使用流式方式
ImmutableListMultimap<String, Person> grouped = Multimaps.index(
    people, Person::getDepartment
);

4. 完整示例

复制代码
import java.util.*;
import java.util.stream.Collectors;

class Person {
    private String name;
    private String department;
    private int age;
    private int salary;
    
    // 构造方法、getter、setter
    
    public static void main(String[] args) {
        List<Person> people = Arrays.asList(
            new Person("Alice", "IT", 25, 5000),
            new Person("Bob", "HR", 30, 4500),
            new Person("Charlie", "IT", 28, 5500),
            new Person("David", "HR", 35, 4800)
        );
        
        // 1. 按部门分组
        Map<String, List<Person>> byDept = people.stream()
            .collect(Collectors.groupingBy(Person::getDepartment));
        
        // 2. 按部门统计平均工资
        Map<String, Double> avgSalaryByDept = people.stream()
            .collect(Collectors.groupingBy(
                Person::getDepartment,
                Collectors.averagingDouble(Person::getSalary)
            ));
        
        // 3. 按年龄段分组
        Map<String, List<Person>> byAgeGroup = people.stream()
            .collect(Collectors.groupingBy(p -> {
                if (p.getAge() < 30) return "青年";
                else if (p.getAge() < 40) return "中年";
                else return "资深";
            }));
    }
}

5. 自定义分组逻辑

复制代码
// 自定义分组器
Collector<Person, ?, Map<String, List<Person>>> customGrouping = 
    Collector.of(
        HashMap::new,  // 供应者
        (map, person) -> {  // 累加器
            String key = person.getDepartment() + "-" + 
                        (person.getAge() / 10 * 10);  // 按10岁分段
            map.computeIfAbsent(key, k -> new ArrayList<>())
               .add(person);
        },
        (map1, map2) -> {  // 合并器(用于并行流)
            map2.forEach((key, value) -> 
                map1.merge(key, value, (list1, list2) -> {
                    list1.addAll(list2);
                    return list1;
                })
            );
            return map1;
        }
    );

Map<String, List<Person>> customGrouped = people.stream()
    .collect(customGrouping);

选择建议:

  1. Java 8+ :优先使用 Stream API+ Collectors.groupingBy(),代码简洁高效

  2. 复杂分组:使用多级分组或自定义分组逻辑

  3. 并行处理 :考虑使用并行流 parallelStream()

  4. 性能考虑:大数据量时注意选择合适的 Map 实现(如 LinkedHashMap 保持顺序)

这些方法可以根据具体需求灵活选择和使用。

相关推荐
m0_748554816 小时前
golang如何实现用户订阅偏好管理_golang用户订阅偏好管理实现总结
jvm·数据库·python
lee_curry6 小时前
第四章 jvm中的垃圾回收器
java·jvm·垃圾收集器
smj2302_796826527 小时前
解决leetcode第3911题.移除子数组元素后第k小偶数
数据结构·python·算法·leetcode
阿正呀7 小时前
Redis怎样实现本地缓存的高效失效通知
jvm·数据库·python
九转成圣7 小时前
Java 性能优化实战:如何将海量扁平数据高效转化为类目字典树?
java·开发语言·json
2501_901200538 小时前
mysql如何设置InnoDB引擎参数_优化innodb_buffer_pool
jvm·数据库·python
_.Switch8 小时前
东方财富股票数据JS逆向:secids字段和AES加密实战
开发语言·前端·javascript·网络·爬虫·python·ecmascript
Mr_sst8 小时前
Claude Code 部署与使用保姆级教程(2026 最新)
python·ai
直奔標竿8 小时前
Java开发者AI转型第二十七课!Spring AI 个人知识库实战(六)——全栈闭环收官,解锁前端流式渲染终极技巧
java·开发语言·前端·人工智能·后端·spring
瞎某某Blinder8 小时前
DFT学习记录[6]基于 HES06的能带计算+有效质量计算
python·学习·程序人生·数据挖掘·云计算·学习方法