英文:
Understanding reduction operation of accumulator - java 8
问题
我正在尝试理解减少累加器操作:在下面的示例中
List<String> letters = Arrays.asList("a","bb","ccc");
String result123 = letters
.stream()
.reduce((partialString, element) ->
partialString.length() < element.length()
? partialString
: element
).get();
System.out.println(result123);
partialString是否初始化为空字符串?由于这是一个折叠操作,我认为操作应该结果为空字符串,但它打印出"a"。有人可以解释一下这个累加器是如何工作的吗?
英文:
I m trying to understand reduction accumulator operation: In the below example
List<String> letters = Arrays.asList("a","bb","ccc");
String result123 = letters
.stream()
.reduce((partialString, element) ->
partialString.length() < element.length()
? partialString
: element
).get();
System.out.println(result123);
Is partialString initialized to empty string? Since its a fold operation, I assume that operation should result empty string but its printing "a"
. Can someone please explain how this accumulator works?
答案1
得分: 6
reduce
操作对应的 for-loop
代码如下:
boolean seen = false;
String acc = null;
for (String letter : letters) {
if (!seen) {
seen = true;
acc = letter;
} else {
acc = acc.length() < letter.length() ? acc : letter;
}
}
第一对要进行 reduce 的元素是 (firstElt, secondElt)
,没有空的初始元素。
如果打印 reduce
的每一步:
letters.stream()
.reduce((partialString, element) -> {
System.out.println(partialString + " " + element);
return partialString.length() < element.length() ? partialString : element;
}).get();
// 输出
a bb
a ccc
英文:
The for-loop
corresponding code for the reduce
operation is like
boolean seen = false;
String acc = null;
for (String letter : letters) {
if (!seen) {
seen = true;
acc = letter;
} else {
acc = acc.length() < letter.length() ? acc : letter;
}
}
The first pair of element to reduce are (firstElt, secondElt)
, there is no empty initial element
If you print each step of the reduce
letters.stream()
.reduce((partialString, element) -> {
System.out.println(partialString + " " + element);
return partialString.length() < element.length() ? partialString : element;
}).get();
// output
a bb
a ccc
答案2
得分: 6
如果您阅读文档,即 reduce()
方法的 javadoc,您会了解到 partialString
被初始化为第一个值,而 reduce()
仅用于组合值,即将它们进行缩减。
> partialString
是否被初始化为空字符串?
不是的。如果您想要那样,您需要使用另一个 reduce()
方法,这样您就不需要调用 get()
:
String result123 = letters
.stream()
.reduce("", (partialString, element) ->
partialString.length() < element.length()
? partialString
: element
);
当然,这毫无意义,因为现在 partialString
总是一个 length() = 0
的字符串,所以表达式的结果始终是空字符串。您也可以直接写成 String result123 = "";
并节省所有的 CPU 时间。
英文:
If you read the documentation, i.e. the javadoc of reduce()
, you will learn that partialString
is initialized to the first value, and reduce()
is only called to combine values, aka to reduce them.
> Is partialString
initialized to empty string?
No. If you wanted that, you need to use the other reduce()
method, so you wouldn't need to call get()
:
String result123 = letters
.stream()
.reduce("", (partialString, element) ->
partialString.length() < element.length()
? partialString
: element
);
Of course, that doesn't make any sense, because partialString
is now always a string with length() = 0
, so result of expression is always an empty string. You might as well just write String result123 = "";
and save all the CPU time.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论