2

given List<Integer> count, I want to produce String[] countStr using java 8.

example:
input

List<Integer> count = [1,2,3,4];

output

String[] countStr = ["1","2","3","4"]

I am familar with how to do this using for-each in java 7. I am wondering if I can get a concise way of doing this using java 8.

This is what I tried.

String[] countStr = count.stream().map(String::valueOf).toArray();

But I get compile error, that the above toArray() method returns Object[].

Based on the comments, I tried the following:

List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);

        Stream<Integer> streamString = list.stream();
        String[] stringArray = streamString.toArray(size -> new String[size]);

        System.out.println(Arrays.toString(stringArray));

I get

Exception in thread "main" java.lang.ArrayStoreException: java.lang.Integer
    at java.util.stream.Nodes$FixedNodeBuilder.accept(Nodes.java:1222)
    at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1374)
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:512)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:502)
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:576)
    at java.util.stream.AbstractPipeline.evaluateToArrayNode(AbstractPipeline.java:255)
    at java.util.stream.ReferencePipeline.toArray(ReferencePipeline.java:438)
    at TestDateParser.JavaApi(TestDateParser.java:40)
    at TestDateParser.main(TestDateParser.java:12)

Update: This worked.

count.stream().map(String::valueOf).toArray(size -> new String[size]);

10
  • 1
    I dont understand how this is duplicate? the post mentioned does not answer what I am looking for. whoever marked duplicate does not even read through the question and simply marks duplicate in 30 secs. I am looking for List of Integers to Array of String. Commented Apr 1, 2016 at 20:08
  • 1
    It is a duplicate. count.stream().map(String::valueOf) (your code) is a Stream<String>, so you want to convert a Stream<String> to a String[]. The marked duplicate tells you how to do that. Commented Apr 1, 2016 at 20:14
  • 1
    @Tunaki: it is not stream conversion. It is object casting. my input is Integer and output should be string. Commented Apr 1, 2016 at 20:27
  • 1
    @brainstorm Do count.stream().map(String::valueOf).toArray(size -> new String[size]); This is just a combination of your original question and the answer in the duplicate. Commented Apr 1, 2016 at 20:27
  • 3
    There is no object casting here. Keep it plain / simple. Say again in English what you want to do, and then write. What do you want to do? You want to 1. Convert each integer into a String 2. Make an array out of that. Well 1. is answered by what you already have, i.e. map(String::valueOf) and 2. is answered by the dupe .toArray(String[]::new). Commented Apr 1, 2016 at 20:30

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.