news 2026/7/10 13:05:03

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/7/10 11:42:54

时序数据选型、存储模型与选型

时序数据选型、存储模型与选型 一、时序数据的特征与挑战 时间戳驱动&#xff1a;数据天然带有时间维度&#xff0c;典型场景包括监控指标、传感器采集、交易日志。高吞吐写入&#xff1a;数据持续产生&#xff0c;要求数据库具备批量写入与乱序处理能力。查询模式特殊&#xf…

作者头像 李华
网站建设 2026/7/10 12:57:03

基于微信小程序的家政服务系统

博主介绍&#xff1a;java高级开发&#xff0c;从事互联网行业六年&#xff0c;熟悉各种主流语言&#xff0c;精通java、python、php、爬虫、web开发&#xff0c;已经做了多年的设计程序开发&#xff0c;开发过上千套设计程序&#xff0c;没有什么华丽的语言&#xff0c;只有实…

作者头像 李华
网站建设 2026/7/9 15:47:03

MindSpore高效训练指南:从数据流水线到混合精度实战

在昇腾&#xff08;Ascend&#xff09;NPU上进行深度学习模型训练时&#xff0c;我们经常会遇到GPU转NPU的代码迁移问题&#xff0c;或者发现算力虽然强劲&#xff0c;但训练速度受限于IO或显存。作为一名在昇腾生态摸爬滚打的开发者&#xff0c;今天我想分享几个基于MindSpore…

作者头像 李华
网站建设 2026/7/10 11:04:27

两阶段鲁棒优化在主动配电网动态无功优化中的实践

两阶段鲁棒优化的主动配电网动态无功优化 关键词&#xff1a;两阶段鲁棒优化&#xff0c;CCG算法&#xff0c;储能 仿真算例采用33节点&#xff0c;采用matlabyalmipcplex编写&#xff0c;两阶段模型采用CCG算法求解。 模型中一阶段变量主要包括01变量和无功优化变量&#xff0…

作者头像 李华
网站建设 2026/7/9 23:09:16

探索 DSPLLC 开关电源模块设计的宝藏世界

DSPLLC开关电源模块设计资料DSP数字LLC电源源代码原理图软件学习&#xff0c;包含磁件设计、软件设计报告、硬件设计报告、硬件原理、主功率计算书、LLC环路设计、仿真、BOM、使用说明&#xff0c;调试波形等全面且详细的全套资料最近在研究电源相关的技术&#xff0c;发现了一…

作者头像 李华
网站建设 2026/7/10 9:42:09

基于推荐算法的校园电子图书听书系统

Spring Boot基于推荐算法的校园电子图书听书系统是一个专为校园师生设计的数字化阅读平台。以下是对该系统的详细介绍&#xff1a; 一、系统背景与目的 随着信息技术的不断发展&#xff0c;数字化阅读已经成为校园阅读的新趋势。为了满足校园师生对电子图书和听书资源的需求&am…

作者头像 李华