理解累加器的归约操作 – Java 8

huangapple go评论100阅读模式
英文:

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&lt;String&gt; letters = Arrays.asList(&quot;a&quot;,&quot;bb&quot;,&quot;ccc&quot;);
String result123 = letters
  .stream()
  .reduce((partialString, element) -&gt; 
      partialString.length() &lt; 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 &quot;a&quot;. 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() &lt; 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) -&gt; {
           System.out.println(partialString + &quot; &quot; + element);
           return partialString.length() &lt; 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(&quot;&quot;, (partialString, element) -&gt; 
      partialString.length() &lt; 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 = &quot;&quot;; and save all the CPU time.

huangapple
  • 本文由 发表于 2020年9月23日 02:27:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/64015679.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定