Reputation: 2516
I have a class with long field (primitive type):
class Transfer {
private long id;
//gets sets
}
I want to get a List<Long>
from Collection<Transfer>
which would contain all ids from Collection<Transfer>
e.g.
Collection<Transfer> transfers = ..;
List<Long> ids = (List<Long>) transfers.stream().map(f -> f.getId()).collect(Collectors.toList());
The thing which confuses me is this ugly cast:
(List<Long>) transfers.stream()
Is there any way to avoid it?
Upvotes: 0
Views: 1024
Reputation: 100139
Assuming that your getId()
method return type is long
or Long
, this cast is absolutely unnecessary. You can write
List<Long> ids = transfers.stream().map(f -> f.getId()).collect(Collectors.toList());
Upvotes: 1