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

相关推荐
databook15 分钟前
新数据到手,先跑哪几步检查
python·数据分析
czq_26867194871 小时前
Python打卡第46天
开发语言·python
Flynt2 小时前
日涨4000星的OSINT工具,我拿Linus的用户名和一个随机字符串实测了一遍
python·安全
2601_967987372 小时前
安装 Redis 或 Redis Stack
linux·windows·redis·macos·安装
曲径海盗4 小时前
【Python 学习第 53 天】列表基础
python·学习笔记·编程入门
Dumbness5 小时前
群里 @ 机器人发"查订单",指令一条都没命中:微信群机器人在路由前漏掉的几步归一化
python·微信
用户1614840955455 小时前
用 M4 Pro 跑羽毛球视频 AI 分析(二):TrackNet MPS 加速 3.2 倍、卡尔曼滤波补漏与自动回合剪辑
python
用户4019925568615 小时前
基于对话树回溯的社媒评论流买方意图识别与噪音过滤实践
python
用户0332126663676 小时前
使用 Python 加密或解密 PDF 文档
python
小七-七牛开发者6 小时前
让 Go 走得更远:用 LLGo 打通 Go 与 C/C++/Python 生态
python·编程·编译器·llvm·llgo