news 2026/8/5 17:07:24

Java 中将 List 中对象的某一列转换为 Set

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Java 中将 List 中对象的某一列转换为 Set

在 Java 中将 List 中对象的某一列转换为 Set,有几种常用方法:

1. 使用 Stream API(最常用)

import java.util.*; import java.util.stream.Collectors; // 示例类 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; } } public class Main { public static void main(String[] args) { List<Person> personList = Arrays.asList( new Person("张三", 25), new Person("李四", 30), new Person("王五", 25), new Person("张三", 28) // 重复的张三 ); // 方法1:提取 name 列到 Set(自动去重) Set<String> nameSet = personList.stream() .map(Person::getName) // 提取 name .collect(Collectors.toSet()); System.out.println(nameSet); // [张三, 李四, 王五] // 方法2:提取 age 列到 Set Set<Integer> ageSet = personList.stream() .map(Person::getAge) .collect(Collectors.toSet()); System.out.println(ageSet); // [25, 30, 28] } }

2. 指定具体的 Set 实现

// 使用 HashSet Set<String> nameSet = personList.stream() .map(Person::getName) .collect(Collectors.toCollection(HashSet::new)); // 使用 TreeSet(排序) Set<String> sortedNameSet = personList.stream() .map(Person::getName) .collect(Collectors.toCollection(TreeSet::new)); // 使用 LinkedHashSet(保持插入顺序) Set<String> linkedNameSet = personList.stream() .map(Person::getName) .collect(Collectors.toCollection(LinkedHashSet::new));

3. 处理可能为 null 的情况

// 方法1:过滤掉 null Set<String> nameSet = personList.stream() .map(Person::getName) .filter(Objects::nonNull) // 过滤 null .collect(Collectors.toSet()); // 方法2:使用 filter 和 Optional Set<String> nameSet = personList.stream() .map(Person::getName) .filter(name -> name != null && !name.trim().isEmpty()) // 过滤 null 和空字符串 .collect(Collectors.toSet());

4. 复杂对象属性提取

// 如果属性是嵌套对象 class Department { private String deptName; // getters and setters } class Employee { private String name; private Department department; // getters and setters } // 提取嵌套属性 Set<String> deptNames = employeeList.stream() .map(Employee::getDepartment) .filter(Objects::nonNull) .map(Department::getDeptName) .collect(Collectors.toSet());

5. 并行流处理(大数据量时)

Set<String> nameSet = personList.parallelStream() // 并行处理 .map(Person::getName) .collect(Collectors.toSet());

6. 传统方法(不使用 Stream)

// 传统 for 循环 Set<String> nameSet = new HashSet<>(); for (Person person : personList) { nameSet.add(person.getName()); } // 传统 for 循环,处理 null Set<String> nameSet = new HashSet<>(); for (Person person : personList) { if (person.getName() != null) { nameSet.add(person.getName()); } }

主要区别对比

方法

优点

缺点

Stream API

代码简洁,可读性好,支持链式调用

Java 8+

并行流

大数据量性能好

线程安全需注意

传统循环

兼容性好,Java 8 以下可用

代码冗长

最佳实践建议

  1. 推荐使用 Stream API:代码简洁,可读性好

  2. 考虑使用 LinkedHashSet:如果需要保持顺序

  3. 总是处理 null 值:避免 NullPointerException

  4. 大数据量考虑并行流:但要注意线程安全问题

  5. 使用具体类型:明确指定 Set 的实现类型,便于维护

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

vue导出excel文件

Vue.js 本身不提供直接导出 Excel 的功能&#xff0c;但可以通过以下几种方式实现&#xff1a; 1. 前端导出方案 使用 xlsx 库&#xff08;推荐&#xff09; npm install xlsx # 或 yarn add xlsx <template><button click"exportExcel">导出Excel&l…

作者头像 李华
网站建设 2026/8/5 6:54:24

基于STM32的自动售货机控制系统设计

第一章&#xff1a;系统核心硬件架构与选型 基于STM32的自动售货机控制系统以“高效交易、稳定出货”为核心&#xff0c;采用STM32F103ZET6作为主控芯片&#xff0c;其32位Cortex-M3内核与512KB Flash可满足多通道控制与支付交互需求。核心模块包括&#xff1a; 货道控制&#…

作者头像 李华
网站建设 2026/8/5 7:44:22

液压挖掘机回转能量回收系统设计与仿真

一、系统整体方案设计 液压挖掘机回转能量回收系统以“能量回收-存储-再利用”为核心逻辑&#xff0c;适配挖掘机回转制动阶段的能量特性&#xff0c;整体采用“液压蓄能电控调节”的混合架构。系统在原有回转液压回路基础上&#xff0c;新增能量回收支路与释放支路&#xff1a…

作者头像 李华
网站建设 2026/8/5 6:25:05

android 媒体之 MediaSession

一、框架1. MediaSession (媒体会话) • 角色&#xff1a; 核心枢纽和状态机。它是整个媒体播放控制的核心。 • 作用&#xff1a; 封装播放状态&#xff1a; 持有当前播放状态&#xff08;如播放/暂停、当前播放位置、播放速度、播放队列、当前媒体项元数据等&#xff09;…

作者头像 李华
网站建设 2026/8/5 8:40:25

校园网络规划

一、需求分析与总体目标 校园网络规划需综合考虑教学、科研、管理和生活等多场景需求&#xff0c;构建高速、稳定、安全、可扩展的网络基础设施。 主要需求&#xff1a; 带宽需求&#xff1a;支持高清视频教学、在线实验、大规模文件传输覆盖范围&#xff1a;教学楼、实验室、图…

作者头像 李华
网站建设 2026/8/5 12:07:09

护眼灯已足够优秀,为何仍需眼调节训练灯?答案藏在近视防控里

如今的优质护眼灯&#xff0c;早已是家庭学习场景中的“性能王者”——照度远超国标要求&#xff0c;确保读写区域光线均匀充足&#xff0c;无明显明暗死角&#xff1b;达到RG0蓝光豁免级标准&#xff0c;从根源上杜绝蓝光对眼睛带来的潜在伤害&#xff1b;搭配高显色指数与无感…

作者头像 李华