news 2026/5/13 21:03: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/5/10 14:09:17

探讨IEEE39节点系统中的暂态稳定分析

IEEE39节点标准系统&#xff0c;标准算例数据&#xff0c;电源采用发电机模型&#xff0c;更能考虑完备暂态响应。这个是相量模型&#xff08;phasor&#xff09;&#xff0c;这个适合用于暂态稳定分析&#xff0c;也可以进行短路分析&#xff0c;自加风机光伏等&#xff0c;无…

作者头像 李华
网站建设 2026/5/10 2:47:33

操作教程丨搭建MaxKB图文混合文档分析工作流,轻松分析带图片的文档

MaxKB开源企业级智能体平台的高级编排中提供了文档内容提取组件&#xff0c;其主要输出的是Markerdown格式的文本内容。然而&#xff0c;当文档内嵌架构图、流程图、图表等图片信息时&#xff0c;这些视觉内容就无法被有效提取&#xff0c;导致后续大语言模型&#xff08;LLM&a…

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

探秘电机低速无感速度矢量控制:高频方波电压注入法

该模型在d轴注入高频的方波电压&#xff0c;在静止坐标下通过前后周期的电电流相应提取高频和低频电流分量&#xff0c;无需额外的数字滤波&#xff0c;得到电流误差经过锁相环PLL观测到电机的位置和速度信息&#xff0c;用于低速下的无感速度矢量控制。在电机控制领域&#xf…

作者头像 李华
网站建设 2026/5/9 18:29:03

在Lumerical FDTD中复现不对称光栅的衍射效率

Lumerical FDTD 复现不对称光栅的衍射效率最近在研究光学相关内容时&#xff0c;遇到了不对称光栅的衍射效率问题。利用Lumerical FDTD来复现这一现象&#xff0c;感觉像是在构建一个微观的光学魔法世界&#xff0c;每一个参数和设置都像是魔法咒语&#xff0c;精准地调控着光线…

作者头像 李华
网站建设 2026/5/11 7:57:36

综合能源系统零碳优化调度研究附Matlab代码

✅作者简介&#xff1a;热爱科研的Matlab仿真开发者&#xff0c;擅长数据处理、建模仿真、程序设计、完整代码获取、论文复现及科研仿真。&#x1f34e; 往期回顾关注个人主页&#xff1a;Matlab科研工作室&#x1f34a;个人信条&#xff1a;格物致知,完整Matlab代码及仿真咨询…

作者头像 李华
网站建设 2026/5/9 6:58:38

现代前端工程化实战:从 Vite 到 React Router demo的构建之旅

前端技术的迭代从未停歇。当我们谈论现代前端开发时&#xff0c;React 19 和 Vite 已经成为了不可忽视的标准配置。React 19 带来了更高效的并发渲染机制&#xff0c;而 Vite 则凭借基于 ESM 的极致冷启动速度&#xff0c;彻底改变了开发体验。 本文将通过一个名为 react-demo…

作者头像 李华