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

相关推荐
染指11106 小时前
26.RAG进阶(Advanced RAG)-假设性问题索引
人工智能·windows·agent·rag·advanced rag
闵孚龙6 小时前
动态图机制:为什么 PyTorch 调试起来更舒服
人工智能·pytorch·python
chushiyunen7 小时前
langchain4j笔记、tools
笔记·python·flask
程序员三藏8 小时前
Web自动化测试详解
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
在放️8 小时前
Python 爬虫 · 第三方代理接入与合规使用
开发语言·爬虫·python
财经资讯数据_灵砚智能9 小时前
基于全球经济类多源新闻的NLP情感分析与数据可视化(夜间-次晨)2026年6月14日
大数据·人工智能·python·ai·信息可视化·自然语言处理·灵砚智能
就改了9 小时前
Windows 环境 SkyWalking 完整实操教程
windows·微服务·skywalking
JOJO数据科学11 小时前
JupyterLab Electron 鸿蒙 PC 适配全记录:从 Python 原生崩溃到 node-static 本地工作台
python·electron·harmonyos
xufengzhu11 小时前
第三方 Python 库 redis-py + hiredis 的使用
开发语言·redis·python
llxxyy卢12 小时前
polar夏季赛部分题目
开发语言·python