0

I have this array:

Integer[] originalItems = itemsArray.stream()
            .distinct()
            .sorted()
            .toArray(Integer[]::new);

I would like to return it as int[] rather than as Integer[].

I have tried to call .toArray(int[]::new) but I get this error:

no instance(s) of type variable(s) A exist so that int[] conforms to A[]

Stefan Zobel
  • 3,182
  • 7
  • 28
  • 38
Gilbert
  • 2,699
  • 28
  • 29

1 Answers1

6
.mapToInt(Integer::intValue).toArray();

instead of

.toArray(Integer[]::new);

because

<A> A[] toArray(IntFunction<A[]> generator);

doesn't work with primitive types.

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142