Lets say we have this boring piece of code that we all had to use:
ArrayList<Long> ids = new ArrayList<Long>();
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<Long> 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 aList<T>
. Not anArrayList
.Program to the
interface
.From the documentation:
Emphasis mine - you cannot even assume that the
List
returned is mutable, let alone that it is of a specific class. If you want anArrayList
:Note also, that it is customary to use
import static
with the Java 8Stream
API so adding:(I hate starred
import static
, it does nothing but pollute the namespace and add confusion. But selectiveimport static
, especially with the Java 8 utility classes, can greatly reduce redundant code)Would result in: