List<DocumentMetadata> 取所有docid,组成List<String>

List<DocumentMetadata> 提取所有 docid 组成 List<String>

假设 DocumentMetadata 类中有 getDocid() 方法或 docid 字段,以下是几种常见实现方式:

方法一:Stream API(推荐,Java 8+)

复制代码
复制代码
List<String> docIds = list.stream()
    .map(DocumentMetadata::getDocid)
    .collect(Collectors.toList());

完整示例

复制代码
复制代码
import java.util.*;
import java.util.stream.*;

class DocumentMetadata {
    private String docid;
    
    public DocumentMetadata(String docid) {
        this.docid = docid;
    }
    
    public String getDocid() {
        return docid;
    }
}

public class Example {
    public static void main(String[] args) {
        List<DocumentMetadata> list = Arrays.asList(
            new DocumentMetadata("doc1"),
            new DocumentMetadata("doc2"),
            new DocumentMetadata("doc3")
        );
        
        // 提取 docid
        List<String> docIds = list.stream()
            .map(DocumentMetadata::getDocid)
            .collect(Collectors.toList());
        
        System.out.println(docIds); // [doc1, doc2, doc3]
    }
}

方法二:传统 for 循环

复制代码
复制代码
List<String> docIds = new ArrayList<>();
for (DocumentMetadata doc : list) {
    docIds.add(doc.getDocid());
}

方法三:forEach + Lambda

复制代码
复制代码
List<String> docIds = new ArrayList<>();
list.forEach(doc -> docIds.add(doc.getDocid()));

方法对比

方法 代码简洁度 性能 推荐度
Stream API ⭐⭐⭐⭐⭐ ✅ 推荐
for 循环 ⭐⭐⭐ 最高 兼容性好
forEach ⭐⭐⭐⭐ 可选

进阶处理

1. 过滤 null 值

复制代码
复制代码
List<String> docIds = list.stream()
    .map(DocumentMetadata::getDocid)
    .filter(Objects::nonNull)
    .collect(Collectors.toList());

2. 去重

复制代码
复制代码
List<String> docIds = list.stream()
    .map(DocumentMetadata::getDocid)
    .distinct()
    .collect(Collectors.toList());

3. 过滤空字符串

复制代码
复制代码
List<String> docIds = list.stream()
    .map(DocumentMetadata::getDocid)
    .filter(s -> s != null && !s.isEmpty())
    .collect(Collectors.toList());

4. 转成不可变列表(Java 10+)

复制代码
复制代码
List<String> docIds = list.stream()
    .map(DocumentMetadata::getDocid)
    .collect(Collectors.toUnmodifiableList());

注意事项

  1. 空列表处理:如果原列表可能为 null,先判空

    复制代码
    复制代码
    List<String> docIds = list == null ? new ArrayList<>() : 
        list.stream().map(DocumentMetadata::getDocid).collect(Collectors.toList());
  2. 性能:数据量大时 Stream 性能略低于 for 循环,但差异很小

  3. 并行处理 :大数据量可用 parallelStream()

    复制代码
    复制代码
    List<String> docIds = list.parallelStream()
        .map(DocumentMetadata::getDocid)
        .collect(Collectors.toList());

✅ 最佳实践

复制代码
复制代码
// 推荐写法:简洁、安全、易读
List<String> docIds = Optional.ofNullable(list)
    .orElse(Collections.emptyList())
    .stream()
    .map(DocumentMetadata::getDocid)
    .filter(Objects::nonNull)
    .collect(Collectors.toList());

推荐使用 Stream API,代码简洁且功能强大!

相关推荐
萝卜白菜。5 分钟前
TongWeb7.0相同的类指明加载顺序
开发语言·python·pycharm
赵钰老师14 分钟前
【ADCIRC】基于“python+”潮汐、风驱动循环、风暴潮等海洋水动力模拟实践技术应用
python·信息可视化·数据分析
爬山算法28 分钟前
MongoDB(80)如何在MongoDB中使用多文档事务?
数据库·python·mongodb
YuanDaima20481 小时前
基于 LangChain 1.0 的检索增强生成(RAG)实战
人工智能·笔记·python·langchain·个人开发·langgraph
RopenYuan2 小时前
FastAPI -API Router的应用
前端·网络·python
听风吹等浪起2 小时前
用Python和Pygame从零实现坦克大战
开发语言·python·pygame
_MyFavorite_2 小时前
JAVA重点基础、进阶知识及易错点总结(28)接口默认方法与静态方法
java·开发语言·windows
书到用时方恨少!2 小时前
Python Pandas 使用指南:数据分析的瑞士军刀
python·数据分析·pandas
智算菩萨3 小时前
【Pygame】第8章 文字渲染与字体系统(支持中文字体)
开发语言·python·pygame
:mnong3 小时前
全图纸语义理解升级分析
python·openvino·paddleocr·qt6.3·paddleocr-vl