Rilcon42
Rilcon42

Reputation: 9763

Convert Map <Integer, Long> to array of Integer

Can someone tell me what I am doing wrong here? p.getVote(), and the collection counting logic return a Long, but I am trying to make my end output an array of ints.

Map<Integer, Long> counters = iqr.getQgagueUniqueVotes().stream()
                               .collect(Collectors.groupingBy(p -> ((int)p.getVote()), 
                                    Collectors.counting()));
Collection<Long> values = counters.values();
long[] targetArray = values.toArray(new Long[values.size()]);

Error:

Incompatible type: Inference variable has incompatible upper bound

Upvotes: 2

Views: 73

Answers (1)

Ousmane D.
Ousmane D.

Reputation: 56453

Change the type of the targetArray array to a type Long:

Long[] targetArray = values.toArray(new Long[values.size()]);

or create a stream of the values and map to a type long then collect to an array.

long[] targetArray = values.stream().mapToLong(Long::longValue).toArray();

Upvotes: 3

Related Questions