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
  • 132,869
  • 46
  • 340
  • 423
richersoon
  • 4,682
  • 13
  • 44
  • 74

2 Answers2

9
String[] array = Stream.of(ids).map(String::valueOf).toArray(String[]::new);
Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • 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 – Vince 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
  • 776
  • 4
  • 19