collect() java,如何在Java 8中使用collect调用?

Lets say we have this boring piece of code that we all had to use:

ArrayList ids = new ArrayList();

for (MyObj obj : myList){

ids.add(obj.getId());

}

After switching to Java 8, my IDE is telling me that I can replace this code with collect call, and it auto-generates:

ArrayList ids = myList.stream().map(MyObj::getId).collect(Collectors.toList());

However its giving me this error:

collect(java.util.stream.Collector) in Steam cannot be applied to: (java.util.stream.Collector, capture, java.util.List>)

I tried casting the parameter but its giving me undefined A and R, and the IDE isn't giving any more suggestions.

I'm curious as how can you use collect call in this scenario, and I couldn't find any information that could guide me properly. Can anyone shed a light?

解决方案

The issue is that Collectors.toList, not surprisingly, returns a List. Not an ArrayList.

List ids = remove.stream()

.map(MyObj::getId)

.collect(Collectors.toList());

Program to the interface.

From the documentation:

Returns a Collector that accumulates the input elements into a new

List. There are no guarantees on the type, mutability,

serializability, or thread-safety of the List returned; if more

control over the returned List is required, use

toCollection(Supplier).

Emphasis mine - you cannot even assume that the List returned is mutable, let alone that it is of a specific class. If you want an ArrayList:

ArrayList ids = remove.stream()

.map(MyObj::getId)

.collect(Collectors.toCollection(ArrayList::new));

Note also, that it is customary to use import static with the Java 8 Stream API so adding:

import static java.util.stream.Collectors.toCollection;

(I hate starred import static, it does nothing but pollute the namespace and add confusion. But selective import static, especially with the Java 8 utility classes, can greatly reduce redundant code)

Would result in:

ArrayList ids = remove.stream()

.map(MyObj::getId)

.collect(toCollection(ArrayList::new));

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值