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,代码简洁且功能强大!

相关推荐
wj3055853788 小时前
课程 9:模型测试记录与 Prompt 策略
linux·人工智能·python·comfyui
星寂樱易李8 小时前
iperf3 + Python-- 网络带宽、网速、网络稳定性
开发语言·网络·python
qingfeng154159 小时前
企业微信机器人开发:如何实现自动化与智能运营?
人工智能·python·机器人·自动化·企业微信
彦为君12 小时前
Agent 安全:从权限提示到沙箱隔离
python·ai·ai编程
PILIPALAPENG12 小时前
Python 语法速成指南:前端开发者视角(JS 类比版)
前端·人工智能·python
用户83562907805113 小时前
Python 操作 PowerPoint 页眉与页脚指南
后端·python
枫叶林FYL14 小时前
项目九:异步高性能爬虫与数据采集中枢 —— 基于 Crawl<sub>4</sub>AI 与 Playwright 的现代化数据采集平台 项目总览
爬虫·python·深度学习·wpf
猫猫的小茶馆14 小时前
【Python】函数与模块化编程
linux·开发语言·arm开发·驱动开发·python·stm32
Miss_min15 小时前
128K长序列数据生成
开发语言·python·深度学习