java-8 - 使用 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());
这是重构上述代码的正确方法吗?请建议重构上述代码的最佳方法。
解决方案
问题是,lambda 表达式仍然相当复杂。如果我假装它在提取到方法时是隐藏的,那么保留在重构版本中的只是通过流将一个列表转换为另一个列表。很好,但也许不需要。我会将该块 lambda 折射到几个函数,以获得整体上更易于理解的代码(希望如此)。
我强烈推荐以下视频:
Venkat Subramaniam https://youtu.be/1OpAgZvYXLQ品尝 Lambdas 并沉迷于 Streams
推荐阅读
- javascript - 使用 jquery 输入后查找具有特定类的跨度
- r - 将时间间隔绘制为段
- javascript - 链接多个级别的条件承诺
- reactjs - Webpack:使用 DefinePlugin 和 DotEnv 未定义的 process.env
- c# - C# AWS Lambda 函数异常处理
- gitlab - 如何禁用一种类型的所有服务集成
- python - 在 pyqt5 中使内容和选项卡可扩展
- vba - 通过具有非连续范围的联合的 VBA 循环
- python - python中的FFT给出不同的结果
- amazon-web-services - AWS IAM EC2 策略仅限于原始实例