Convert long list to int java 8 list

I have a method that takes an integer list as a parameter. I currently have a long list and want to convert it to an integer list, so I wrote:

  List<Integer> student =
  studentLong.stream()
  .map(Integer::valueOf)
  .collect(Collectors.toList());

But I got an error message:

method "valueOf" can not be resolved. 

Is it really possible to convert a long list to an integer list?

+4
source share
1 answer

You must use mapToIntwith Long::intValueto extract the value int:

List<Integer> student = studentLong.stream()
           .mapToInt(Long::intValue)
           .boxed()
           .collec‌t(Collectors.toList(‌​))

The reason you get method "valueOf" can not be resolved.it is because there is no signature Integer::valueOfthat takes Longas an argument.

EDIT
Per Holger's comment below, we can also do:

List<Integer> student = studentLong.stream()
           .map(Long::intValue)
           .collec‌t(Collectors.toList(‌​))
+2
source

Source: https://habr.com/ru/post/1689989/


All Articles