Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert a list of long to a list of int java 8

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?

like image 768
user3369592 Avatar asked Nov 29 '17 01:11

user3369592


Video Answer


1 Answers

You should use a mapToInt with Long::intValue in order to extract the int value:

List<Integer> student = studentLong.stream()
           .mapToInt(Long::intValue)
           .boxed()
           .collec‌t(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)
           .collec‌t(Collectors.toList(‌​))
like image 153
Nir Alfasi Avatar answered Sep 28 '22 19:09

Nir Alfasi