英文:
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.<T, CustomLinkedList<T>>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
有三个参数:
- 供应商,对于你的情况是
CustomLinkedList
。 - 用于将元素添加到供应商的函数。
- 在并行流处理时,返回两个不同的
CustomLinkedList
(即list1
和list2
)合并后的结果的函数是必要的。
有关更多详细示例,请参阅此链接。
英文:
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) -> list1.mergeWith(list2)
)
);
Collector.of
has three parameters:
- The supplier to use, in your case
CustomLinkedList
. - The function of how to add elements to your supplier.
- The function that returns the result of two merged different
CustomLinkedList
list1
andlist2
in my example, this function is necessary when using parallel stream processing.
For more details with example see this link.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论