You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

循环内执行多语句时如何运用Java Stream特性?

优化方案:使用Java Stream重构PDF表单字段处理代码

核心思路

将嵌套循环和逐行处理逻辑转化为Stream流水线操作,同时优化字符替换逻辑的可读性与可维护性,消除并行数组带来的潜在同步风险。


步骤1:重构字符替换规则

把原有的两个并行数组替换为键值对集合,避免数组长度不一致导致的错误:

// 用键值对存储特殊字符与替换目标,替代原有的并行数组
List<Map.Entry<String, String>> charReplacements = Arrays.asList(
    new AbstractMap.SimpleEntry<>(" ", "_"),
    new AbstractMap.SimpleEntry<>("-", "_1_"),
    new AbstractMap.SimpleEntry<>(".", "_2_"),
    new AbstractMap.SimpleEntry<>("(", "_3_"),
    new AbstractMap.SimpleEntry<>(")", "_4_"),
    new AbstractMap.SimpleEntry<>("/", "_5_"),
    new AbstractMap.SimpleEntry<>("*", "_6_")
);

步骤2:Stream流水线重构主逻辑

将外层循环转化为Stream操作,把字段名转换、条件判断等逻辑串联成清晰的流水线:

PDAcroForm pdAcroForm = pdfDocument.getDocumentCatalog().getAcroForm();
JSONObject jsonData = new JSONObject(pdfdata);

// 封装字段名转换逻辑为独立函数
Function<String, String> transformFieldName = name -> {
    String trimmedName = name.trim();
    // 批量应用所有字符替换规则
    String replacedName = charReplacements.stream()
        .reduce(trimmedName, (current, entry) -> current.replace(entry.getKey(), entry.getValue()), String::concat);
    // 首字母小写处理
    return StringUtils.isNotEmpty(replacedName) 
        ? replacedName.substring(0, 1).toLowerCase().concat(replacedName.substring(1)) 
        : replacedName;
};

// Stream处理每个表单字段
pdAcroForm.getFields().stream()
    .peek(field -> System.out.println("fieldName:" + field.getFullyQualifiedName())) // 保留原打印逻辑
    .forEach(field -> {
        String transformedName = transformFieldName.apply(field.getFullyQualifiedName());
        if (jsonData.has(transformedName)) {
            String value = jsonData.getString(transformedName);
            field.setValue(value);
            field.setReadOnly(true);
        }
    });

优化点说明

  • 可读性提升:字段名转换逻辑封装为独立函数,主流水线职责清晰,代码结构更易理解。
  • 冗余操作消除:原代码中每次替换都调用trim(),优化后仅在转换开始时执行一次。
  • 风险规避:用键值对集合存储替换规则,彻底避免并行数组长度不一致导致的错误。
  • Stream特性合理运用:用stream()遍历字段,peek()处理打印副作用,forEach()处理字段修改(因涉及可变对象操作,适合用forEach而非映射后收集)。

可选进阶优化

如果表单字段数量庞大,可尝试使用并行Stream提升处理效率(需确认PDF库的字段操作线程安全):

pdAcroForm.getFields().parallelStream()
    // 后续操作同上述流水线

内容的提问来源于stack exchange,提问作者Kishore_2021

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 07:50:28