2

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
Rilcon42
  • 9,584
  • 18
  • 83
  • 167
  • 1
    Is your issue just with converting `Map` to `long[]`? Then you probably should replace the initialisation based on a stream statement (that's also dependent on a variable you didn't show) with some hard-coded data. See also: [mcve]. – Bernhard Barker Feb 25 '18 at 19:46
  • Similar: [How to convert an ArrayList containing Integers to primitive int array?](https://stackoverflow.com/q/718554) – Bernhard Barker Feb 25 '18 at 19:56

1 Answers1

3

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();
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
  • 3
    I recommend [this article](https://shipilev.net/blog/2016/arrays-wisdom-ancients/) regarding the pattern of using `.toArray(new Long[values.size()])` instead of `.toArray(new Long[0])`. – Holger Feb 26 '18 at 08:56