1

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 ?

3
  • 4
    Does this answer your question? Java 8 int array to map Commented Jul 8, 2020 at 17:27
  • 2
    IntStream has only one collect method: docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/…. Try to map it to a Stream<Integer> like this: IntStream.range(0, arr.length).mapToObj(Integer::valueOf).collect(Collectors.toMap(k -> k, k -> Integer.valueOf(arr[k.intValue()])));. Commented Jul 8, 2020 at 17:28
  • Try to use a Stream of Integer by using IntStream#boxed instead. Commented Jul 8, 2020 at 17:34

4 Answers 4

3

Try:

int arr[] = {4,5,6};
Map<Integer, Integer> resultMap = IntStream.range(0, arr.length).boxed()
                    .collect(Collectors.toMap(Function.identity(), k -> arr[k]));
resultMap.entrySet().forEach(entry -> System.out.println(entry.getKey() + " = " + entry.getValue()));

Output:

0 = 4
1 = 5
2 = 6
Sign up to request clarification or add additional context in comments.

6 Comments

Integer#valueOf is not necessary.
@Jason thanks, I have updated answer
No worries, also instead of using k -> k you can just use Function#identity to use the input value of the Stream and the key of the map.
@NitinBisht What is Function.identity() ?
@ng.newbie Function.identity() returns a function that always returns its input argument.
|
2

You are using IntStream which is unboxed. You need to use .boxed().

If your array is ordered, yo can do:

int arr[] = {4,5,6};

Map<Integer, Integer> map = Arrays.stream(arr).boxed()
    .collect(Collectors.toMap(i -> Arrays.binarySearch(arr, i), Function.identity()));

map.entrySet().forEach(entry -> System.out.println(entry.getKey() + " = " + entry.getValue()));

If it's not ordered you can do:

Map<Integer, Integer> map = IntStream.range(0, arr.length).boxed()
        .collect(Collectors.toMap(Function.identity(), i -> arr[i]));

2 Comments

What is Function.identity() ??
It is similar to i -> i (but not the same). From javadoc: "Returns a function that always returns its input argument.". If you want to get more info, try this answer: stackoverflow.com/questions/28032827/…
1

You have to box the IntStream and use groupingBy value to get the wanted count.

Try this:

Map<Integer, Long> map = Arrays
        .stream(arr)
        .boxed()
        .collect(Collectors.groupingBy(e -> e, Collectors.counting()));

Comments

1

There are multiple solutions.

1. For-each loop

Map<Integer, Integer> map = new HashMap<>();
for (int i=0; i<arr.length; i++) {
    map.put(i, arr[i]);
}

In my opinion, this is the most clear in terms of readability.


2. IntStream with a range of the array length

Map<Integer, Integer> map = IntStream.range(0, arr.length)
    .boxed()
    .collect(Collectors.toMap(Function.identity(), i -> arr[i]));

This seems like your original attempt. You need either to box with boxed() to Stream<Integer> in order to use collect with a single Collector.


3. Stream::iterate and Stream::limit

Map<Integer, Integer> map = Stream.iterate(0, i -> i+1)
    .limit(arr.length)
    .collect(Collectors.toMap(Function.identity(), i -> arr[i]));

It's a simple endless generation of an increasing sequence by one limited by the length of the array.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.