Java將List中對象的某一列轉(zhuǎn)換為Set
更新時間:2025年12月19日 08:37:02 作者:悟能不能悟
這篇文章主要為大家詳細(xì)介紹了Java如何實現(xiàn)將List中對象的某一列轉(zhuǎn)換為Set,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
在 Java 中將 List 中對象的某一列轉(zhuǎn)換為 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) // 重復(fù)的張三
);
// 方法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 實現(xiàn)
// 使用 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. 復(fù)雜對象屬性提取
// 如果屬性是嵌套對象
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. 并行流處理(大數(shù)據(jù)量時)
Set<String> nameSet = personList.parallelStream() // 并行處理
.map(Person::getName)
.collect(Collectors.toSet());6. 傳統(tǒng)方法(不使用 Stream)
// 傳統(tǒng) for 循環(huán)
Set<String> nameSet = new HashSet<>();
for (Person person : personList) {
nameSet.add(person.getName());
}
// 傳統(tǒng) for 循環(huán),處理 null
Set<String> nameSet = new HashSet<>();
for (Person person : personList) {
if (person.getName() != null) {
nameSet.add(person.getName());
}
}主要區(qū)別對比
| 方法 | 優(yōu)點 | 缺點 |
|---|---|---|
| Stream API | 代碼簡潔,可讀性好,支持鏈?zhǔn)秸{(diào)用 | Java 8+ |
| 并行流 | 大數(shù)據(jù)量性能好 | 線程安全需注意 |
| 傳統(tǒng)循環(huán) | 兼容性好,Java 8 以下可用 | 代碼冗長 |
最佳實踐建議
- 推薦使用 Stream API:代碼簡潔,可讀性好
- 考慮使用 LinkedHashSet:如果需要保持順序
- 總是處理 null 值:避免 NullPointerException
- 大數(shù)據(jù)量考慮并行流:但要注意線程安全問題
- 使用具體類型:明確指定 Set 的實現(xiàn)類型,便于維護(hù)
到此這篇關(guān)于Java將List中對象的某一列轉(zhuǎn)換為Set的文章就介紹到這了,更多相關(guān)Java List對象轉(zhuǎn)Set內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot利用Redis解決海量重復(fù)提交問題
本文主要介紹了SpringBoot利用Redis解決海量重復(fù)提交問題,介紹了三種常見的解決方案,包括使用Redis計數(shù)器,使用Redis分布式鎖和使用Redis發(fā)布/訂閱機制,感興趣的可以了解一下2024-03-03
SpringBoot詳解整合Spring?Boot?Admin實現(xiàn)監(jiān)控功能
這篇文章主要介紹了SpringBoot整合Spring?Boot?Admin實現(xiàn)服務(wù)監(jiān)控,內(nèi)容包括Server端服務(wù)開發(fā),Client端服務(wù)開發(fā)其中Spring?Boot?Admin還可以對其監(jiān)控的服務(wù)提供告警功能,如服務(wù)宕機時,可以及時以郵件方式通知運維人員,感興趣的朋友跟隨小編一起看看吧2022-07-07

