Stream collect方法

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

Stream collect method

问题

我不想在第二次使用Stream.collect方法中创建新的列表。它应该只添加到先前的列表中。我尝试像下面这样做,但它不起作用。
根据文档,Collectors.toList()总是创建新的列表,并且collect()方法不允许使用旧列表。是否有任何方法可以在第二次中重用我的先前列表(listA)。

List<A> listA = xList.stream().map().collect(Collectors.toList());
yList.stream().map().collect(listA);
英文:

I do not want create new list from Stream.collect method in second time. It should add in previous list only. I am trying to do like below but it is not working.
As per document, Collectors.toList() is always create new list, and collect() method is not allowing old list. Is there any way i can reuse my previous list(listA) in second time.

List&lt;A&gt; listA = xList.stream().map().collect(Collectors.toList());
                 yList.stream().map().collect(**listA**);

答案1

得分: 3

代替使用.collect,使用.forEach(listA::add)

(从技术上讲,你还应该将第一个示例中的Collectors.toList()替换为Collectors.toCollection(ArrayList::new),因为toList()不能保证返回一个可变列表。)

英文:

Instead of using .collect, write .forEach(listA::add).

(Technically, you should also replace Collectors.toList() in the first example with Collectors.toCollection(ArrayList::new), because toList() does not guarantee that it returns a mutable list.)

答案2

得分: 3

以下是翻译好的部分:

  • 第一个方法只是将直接提供的两个列表合并。
  • 第二个方法将两个流连接在一起。
List<A> list = Stream.of(xList, yList).flatMap(List::stream)
     .map(<someFnc>)
     .collect(Collectors.toList());

List<A> list2 = Stream.concat(xList.stream().map(<someFnc>), 
      yList.stream().map(<someFnc>))
     .collect(Collectors.toList());
英文:

There are several ways to accomplish it. Here are two. They are very similar.

  • The first simply flattens the two lists supplied directly.
  • The second concatenates two streams together.
List&lt;A&gt; list = Stream.of(xList, yList).flatMap(List::stream)
     .map(&lt;someFnc&gt;)
	 .collect(Collectors.toList());

List&lt;A&gt; list2 = Stream.concat(xList.stream().map(&lt;someFnc&gt;), 
      yList.stream().map(&lt;someFnc&gt;)
		.collect(Collectors.toList());

</details>



# 答案3
**得分**: 1

以下是翻译好的部分:

```java
你可以像这样做 -

    List&lt;A&gt; listA = xList.stream().map().collect(Collectors.toList());    
    yList.stream().map().collect(Collectors.toCollection(() -&gt; listA));

编辑:
`collect` 方法接受一个 `Collector`,而在内部,`Collectors.toCollection` 将 yList 的元素添加到原始列表中,如下所示 -

    Collector&lt;T, ?, C&gt; toCollection(Supplier&lt;C&gt; collectionFactory) {
        return new CollectorImpl&lt;&gt;(collectionFactory, Collection&lt;T&gt;::add,
                                   (r1, r2) -&gt; { r1.addAll(r2); return r1; },
                                   CH_ID);
    }
英文:

You can do something like this -

List&lt;A&gt; listA = xList.stream().map().collect(Collectors.toList());    
yList.stream().map().collect(Collectors.toCollection(() -&gt; listA));

EDIT :
collect method takes a Collector and internally Collectors.toCollection adds yList element to the original list as -

Collector&lt;T, ?, C&gt; toCollection(Supplier&lt;C&gt; collectionFactory) {
    return new CollectorImpl&lt;&gt;(collectionFactory, Collection&lt;T&gt;::add,
                               (r1, r2) -&gt; { r1.addAll(r2); return r1; },
                               CH_ID);
}

答案4

得分: 1

你可以使用Stream.concat

List<A> listA = Stream.concat(xList.stream().map(), yList.stream().map())
    .collect(Collectors.toList());
英文:

You can use Stream.concat.

List&lt;A&gt; listA = Stream.concat(xList.stream().map(), yList.stream().map())
    .collect(Collectors.toList());

huangapple
  • 本文由 发表于 2020年7月31日 00:46:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63177744.html
匿名

发表评论

匿名网友

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

确定