首页 > 解决方案 > 使用 Streams (Java8) 重构现有代码的问题

问题描述

我是 Streams 的新手并使用 Java 8 版本。我正在尝试使用 Streams 重构我现有的代码。这是代码片段:

       List<AddressAccessibility> addressAccessibility = entity.getAccessibility();
       List<String> entries = new ArrayList<String>();
       for (AddressAccessibility element : addressAccessibility) {

           String verificationHash;
           if (element.getVerification() != null) {
               verificationHash = verificationHashCodeGenerator(element.getVerification());
           } else {
               verificationHash = "No Verification";
           }
           String id = getFirstIdAndCache(element.getId(), IdReferenceTypes.ADDRESSACCESSIBILITY.getValue(),
                   IdReferenceTypes.ID.getValue());
           StringBuilder entryBuilder = new StringBuilder().append(id).append(DELIM)
                   .append(getPrimaryId(healthCareProvider.getId())).append(DELIM)
                   .append(getPrimaryId(element.getSuppliedId())).append(DELIM);
           if (element.getLocationAccessibilityCategory() != null) {
               entryBuilder.append(element.getLocationAccessibilityCategory().getCode()).append(DELIM)
                       .append(element.getLocationAccessibilityCategory().getShortName()).append(DELIM)
                       .append(element.getLocationAccessibilityCategory().getLongName()).append(DELIM);
           } else {
               entryBuilder.append(DELIM).append(DELIM).append(DELIM);
           }
           entryBuilder.append(element.getLocationAccessibilityText()).append(DELIM).append(verificationHash);

           String entry = replaceNullMethod(entryBuilder.toString());
           entries.add(entry);
       }

要使用流重构上面的代码,我试过这样:

Function<AddressAccessibility,String> address = (AddressAccessibility element) -> {

            String verificationHash;
            if (element.getVerification() != null) {
                verificationHash = verificationHashCodeGenerator(element.getVerification());
            } else {
                verificationHash = "No Verification";
            }
            String id = getFirstIdAndCache(element.getId(), IdReferenceTypes.ADDRESSACCESSIBILITY.getValue(),
                    IdReferenceTypes.ID.getValue());
            StringBuilder entryBuilder = new StringBuilder().append(id).append(DELIM)
                    .append(getPrimaryId(healthCareProvider.getId())).append(DELIM)
                    .append(getPrimaryId(element.getSuppliedId())).append(DELIM);
            if (element.getLocationAccessibilityCategory() != null) {
                entryBuilder.append(element.getLocationAccessibilityCategory().getCode()).append(DELIM)
                        .append(element.getLocationAccessibilityCategory().getShortName()).append(DELIM)
                        .append(element.getLocationAccessibilityCategory().getLongName()).append(DELIM);
            } else {
                entryBuilder.append(DELIM).append(DELIM).append(DELIM);
            }
            entryBuilder.append(element.getLocationAccessibilityText()).append(DELIM).append(verificationHash);

            String entry = replaceNullMethod(entryBuilder.toString());
            entries.add(entry);
        } 
}

 entity.getAccessibility().stream().map(address).collect(toList());

这是重构上述代码的正确方法吗?请建议重构上述代码的最佳方法。

标签: java-8iterationjava-streamrefactoring

解决方案


问题是,lambda 表达式仍然相当复杂。如果我假装它在提取到方法时是隐藏的,那么保留在重构版本中的只是通过流将一个列表转换为另一个列表。很好,但也许不需要。我会将该块 lambda 折射到几个函数,以获得整体上更易于理解的代码(希望如此)。

我强烈推荐以下视频:

Venkat Subramaniam https://youtu.be/1OpAgZvYXLQ品尝 Lambdas 并沉迷于 Streams


推荐阅读