您还可以使用Stream.reduce(identity,accumulator,combiner)。
身份
identity是减少函数的初始值
accumulator。
累加器
accumulator减少
identity到
result,
identity如果流是 顺序的 ,这是下一个减少的条件。
合路器
永远不要在 顺序 流中调用此函数。它计算下一个
identity从
identity&
result在 并行 流。
BinaryOperator<String> combinerNeverBeCalledInSequentiallyStream=(identity,t) -> { throw new IllegalStateException("Can't be used in parallel stream");};String result = variables.entrySet().stream() .reduce(templateText , (it, var) -> it.replaceAll(format("\$\{%s\}", var.getKey()) , var.getValue()) , combinerNeverBeCalledInSequentiallyStream);


