Java Streams: 使用Collectors.toCollection将返回的集合修改为自定义类型

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

Java Streams: modifying return collection to a custom type with Collectors.toCollection

问题

如何将返回集合修改为自定义类型的集合?
我有一个自定义的链表对象,我想在对其进行某种操作后替换它。

以下是我想实现的一个简单示例:

myCustomList = myCustomList.stream()
                           .sorted(Comparator.comparing(User::getId))
                           .collect(Collectors.toCollection(CustomLinkedList::new))

我从这段代码中的 CustomLinkedList::new 处得到以下错误:

方法引用中的返回类型不匹配:无法将 CustomLinkedList 转换为 java.util.Collection

我猜我可以将其转换为默认的 LinkedList 对象,然后迭代它,并将节点逐个添加到我的自定义 LinkedList 实现中,但这对我来说不是一个干净的解决方案。

这是我正在使用的 customList 链接:customLinkedList
有没有办法解决这个问题?


更新:

根据 Ismail 的建议,我成功为 Collector.of 实现了如下代码:

.collect(Collector.<T, CustomLinkedList<T>>of(
                     CustomLinkedList::new, 
                     CustomLinkedList::add, 
                     CustomLinkedList::cloneInto));

但现在我得到了以下错误:

java.lang.ArrayIndexOutOfBoundsException: 索引 0 超出了长度 0 的范围


解决方案:

我完全忘记了我在使用具有大小为 0 的 Spliterators.spliterator。将其修正为当前列表的大小后,问题得以解决。谢谢大家!

英文:

How can I modify the return collection to that of a custom type? <br>
I have a custom Linkedlist Object that I would like to replace after doing some type of operation on it.

Here's a simple example of what I would like to achieve:

myCustomList = myCustomList.stream()
                           .sorted(Comparator.comparing(user::getId))
                           .collect(Collectors.toCollection(CustomLinkedList::new))

I get the following error from this CustomlinkedList::new piece of code.

> Bad return type in method reference: cannot convert CustomLinkedList<T> to java.util.Collection<T>

I guess I could just convert it to a default LinkedList Object, iterate over it and add the nodes one by one to my customLinkedList implementation, but that doesnt feel like a clean solution to me.

Here's a link to the customList I am using: customLinkedList <br>
Is there a way to solve this problem?


UPDATE:

Based on Ismail's proposal I managed to get the implementation for Collector.of like so:

.collect(Collector.&lt;T, CustomLinkedList&lt;T&gt;&gt;of(
                                     CustomLinkedList::new, 
                                     CustomLinkedList::add, 
                                     CustomLinkedList::cloneInto));

But now I get the following error:

> java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0


SOLUTION:

I completely forgot I was using Spliterators.spliterator with a set size of 0. Fixing that to the current size of the list fixed the issue. Thank you all!

答案1

得分: 2

你可以使用Collector.of来为collect的Java流定制supplier

myCustomList = myList.stream()
        .sorted(Comparator.comparing(User::getId))
        .collect(Collector.of(
                        CustomLinkedList::new,
                        CustomLinkedList::addNewElement,
                        (list1, list2) -> list1.mergeWith(list2)
                )
        );

Collector.of有三个参数:

  1. 供应商,对于你的情况是CustomLinkedList
  2. 用于将元素添加到供应商的函数。
  3. 在并行流处理时,返回两个不同的CustomLinkedList(即list1list2)合并后的结果的函数是必要的。

有关更多详细示例,请参阅此链接

英文:

You can customize the supplier for the collect java stream using Collector.of:

myCustomList = myList.stream()
        .sorted(Comparator.comparing(user::getId))
        .collect(Collector.of(
                        CustomLinkedList::new,
                        CustomLinkedList::addNewElement,
                        (list1, list2) -&gt; list1.mergeWith(list2)
                )
        );

Collector.of has three parameters:

  1. The supplier to use, in your case CustomLinkedList.
  2. The function of how to add elements to your supplier.
  3. The function that returns the result of two merged different CustomLinkedList list1 and list2in my example, this function is necessary when using parallel stream processing.

For more details with example see this link.

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

发表评论

匿名网友

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

确定