0

I would like to convert an Integer[] but toArray() is giving me Object[]. How can I convert it to String[]?

Stream.of(ids).map(String::valueOf).toArray();
Tunaki
  • 125,519
  • 44
  • 317
  • 399
richersoon
  • 4,262
  • 13
  • 38
  • 70

2 Answers2

9
String[] array = Stream.of(ids).map(String::valueOf).toArray(String[]::new);
Reimeus
  • 155,977
  • 14
  • 207
  • 269
  • I am surprise, Is this a method reference? New keyword can be treat as method? – richersoon Jul 04 '16 at 21:47
  • 1
    @richersoon It's a reference to the constructor – Dioxin Jul 04 '16 at 21:49
  • 1
    method references can be used for constructors as well as methods – Reimeus Jul 04 '16 at 21:49
  • When you have an existing array, it’s recommended to use `Arrays.stream` instead. This will consistently work for `int[]`, `long[]` and `double[]` as well (you only have to use `mapToObj` instead of `map` then), whereas `Stream.of` only works for boxed values and it’s intended use case is the varargs invocation like `Stream.of(a, b, c)`… – Holger Jul 05 '16 at 16:45
-1
String[] a= Arrays.toString(array).split("[\\[\\]]")[1].split(", "); 

I think this should work

Michael
  • 736
  • 4
  • 19