Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a List<Long> to Map<Long, Long> [duplicate]

Tags:

java

java-8

I have the below code and would like to use java 8 to convert a list of Long to a Map<Long,Long>.

Long globalVal= 10;
List<Long> queryLongs = Arrays.asList(600L,700L,800L);
Map<Long, Long> map = queryLongs.stream().collect(Collectors.toMap(i->i, globalVal)));

I get an error when I try to map the individual value in the list as the key of the map.

like image 676
Erica Avatar asked Oct 26 '17 11:10

Erica


1 Answers

The second argument of toMap is also a Function, so you can't just pass globalVal.

Map<Long, Long> map = queryLongs.stream()
                                .collect(Collectors.toMap(Function.identity(), 
                                                          i->globalVal));
like image 110
Eran Avatar answered Sep 18 '22 23:09

Eran