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
  • 2,847
  • 7
  • 25
  • 34
Gilbert
  • 1,591
  • 17
  • 23

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
  • 46,063
  • 13
  • 87
  • 137