code: public static <T> Collector<T, ?, T[]> toArray(IntFunction<T[]> generator) {
          return Collectors.collectingAndThen(Collectors.toList(), list -> list.toArray(generator.apply(list.size())));
      }
docs: Returns a {@code Collector} that accumulates the input elements into a
  new array.
  
  The operation performed by the returned collector is equivalent to
  {@code stream.toArray(generator)}. This collector is mostly useful as a
  downstream collector.
  
  @param <T> the type of the input elements
  @param generator a function which produces a new array of the desired
  type and the provided length
  @return a {@code Collector} which collects all the input elements into an
  array, in encounter order