I have a method which takes a list of integer as parameter. I currently have a list of long and want to convert it to a list of integer so I wrote :
List<Integer> student =
studentLong.stream()
.map(Integer::valueOf)
.collect(Collectors.toList());
But I received an error:
method "valueOf" can not be resolved.
Is it actually possible to convert a list of long to a list of integer?
You should use a mapToInt
with Long::intValue
in order to extract the int
value:
List<Integer> student = studentLong.stream()
.mapToInt(Long::intValue)
.boxed()
.collect(Collectors.toList())
The reason you're getting method "valueOf" can not be resolved.
is because there is no signature of Integer::valueOf
which accepts Long
as an argument.
EDIT
Per Holger's comment below, we can also do:
List<Integer> student = studentLong.stream()
.map(Long::intValue)
.collect(Collectors.toList())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With