I have the following array in Java:
int arr[] = {4,5,6};
I want to convert it into a java.util.Map<K,V>
instance that has the index of the array as keys and the value at index as values for the Map. Like so,
0 = 4
1 = 5
2 = 6
I have tried the following:
IntStream.range(0, arr.length)
.collect(Collectors.toMap(k -> k, k -> arr[k]));
But this results in compilation errors like:
Type mismatch: cannot convert from Collector<Object,capture#1-of ?,Map<Object,Object>> to Supplier<R>
and
The method collect(Supplier<R>, ObjIntConsumer<R>, BiConsumer<R,R>) in the type IntStream is not applicable for the arguments (Collector<Object,?,Map<Object,Object>>)
What am I doing wrong here ?
I am just going over all the indices and then mapping them to keys and values where am I going wrong ?