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();
3
  • 1
    Is it an int[] or an Integer[]? Commented Jul 4, 2016 at 21:40
  • @ElliottFrisch Integer[] Commented Jul 4, 2016 at 21:41
  • Not really a duplicate of the other question. This involves array of Integer[], which is a special case. Commented Jul 19, 2016 at 1:39

2 Answers 2

9
String[] array = Stream.of(ids).map(String::valueOf).toArray(String[]::new);
Sign up to request clarification or add additional context in comments.

4 Comments

I am surprise, Is this a method reference? New keyword can be treat as method?
@richersoon It's a reference to the constructor
method references can be used for constructors as well as methods
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)
-1
String[] a= Arrays.toString(array).split("[\\[\\]]")[1].split(", "); 

I think this should work

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.