我不会说这比您的代码更具可读性,但这表明良好的ol’for循环仍然足够:
List<Data> data = ...;
Map<String, Task> tasks = ...;
UnaryOperator<String> function = s -> {
return Joiner.on(".")
.useForNull("null")
.join(Arrays.copyOfRange(s.split("\\."), 0, 3));
};
data.forEach(datum -> {
final String compKey = datum.getCompKey();
tasks.entrySet()
.stream()
.filter(e -> e.getKey() != null && e.getValue() != null)
.filter(e -> compKey.equals(function.apply(e.getKey())))
.findFirst()
.map(Map.Entry::getValue)
.ifPresent(task -> {
task.setVal1(datum.getVal1());
task.setVal2(datum.getVal2());
task.setVal3(datum.getVal3());
});
});