news 2026/5/1 8:01:06

java list怎么进行group

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
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 保持顺序)

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

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/18 10:15:19

如何快速掌握AEUX插件:设计师的终极效率提升指南

如何快速掌握AEUX插件&#xff1a;设计师的终极效率提升指南 【免费下载链接】AEUX Editable After Effects layers from Sketch artboards 项目地址: https://gitcode.com/gh_mirrors/ae/AEUX 你是否曾经在Figma中精心设计完界面后&#xff0c;却在After Effects中重新…

作者头像 李华
网站建设 2026/4/24 2:06:06

springboot基于vue Web的美容美发理发店管理系统的设计与实现_qqc2rcv4

目录已开发项目效果实现截图开发技术介绍核心代码参考示例1.建立用户稀疏矩阵&#xff0c;用于用户相似度计算【相似度矩阵】2.计算目标用户与其他用户的相似度系统测试总结源码文档获取/同行可拿货,招校园代理 &#xff1a;文章底部获取博主联系方式&#xff01;已开发项目效果…

作者头像 李华
网站建设 2026/5/1 6:07:13

WaveTools鸣潮工具箱完全指南:3步解锁120帧极致体验

WaveTools鸣潮工具箱完全指南&#xff1a;3步解锁120帧极致体验 【免费下载链接】WaveTools &#x1f9f0;鸣潮工具箱 项目地址: https://gitcode.com/gh_mirrors/wa/WaveTools WaveTools鸣潮工具箱是专为《鸣潮》玩家量身打造的实用工具&#xff0c;能够轻松突破游戏性…

作者头像 李华
网站建设 2026/5/1 6:13:40

KH Coder:3步搞定海量文本的智能分析利器

KH Coder&#xff1a;3步搞定海量文本的智能分析利器 【免费下载链接】khcoder KH Coder: for Quantitative Content Analysis or Text Mining 项目地址: https://gitcode.com/gh_mirrors/kh/khcoder 你是不是经常面对堆积如山的文档、客户反馈、社交媒体内容&#xff0…

作者头像 李华
网站建设 2026/4/18 9:14:28

鸣潮工具箱3分钟快速上手:5个超实用技巧提升游戏体验

鸣潮工具箱3分钟快速上手&#xff1a;5个超实用技巧提升游戏体验 【免费下载链接】WaveTools &#x1f9f0;鸣潮工具箱 项目地址: https://gitcode.com/gh_mirrors/wa/WaveTools 鸣潮工具箱是一款专为《鸣潮》游戏玩家设计的PC端优化软件&#xff0c;通过智能性能调节、…

作者头像 李华