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]);

brain storm
  • 30,124
  • 69
  • 225
  • 393
  • 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. – brain storm Apr 01 '16 at 20:08
  • It is duplicate: 1. http://stackoverflow.com/questions/5690351/java-stringlist-toarray-gives-classcastexception 2. http://stackoverflow.com/questions/31030931/java-casting-list-of-strings-to-string-array – Heinzlmaen Apr 01 '16 at 20:13
  • Yes this is a duplicate of the linked question which is more general: it explains how to convert a Stream to an array, which is the real problem here. – Tunaki Apr 01 '16 at 20:13
  • 1
    It is a duplicate. `count.stream().map(String::valueOf)` (your code) is a `Stream`, so you want to convert a `Stream` to a `String[]`. The marked duplicate tells you how to do that. – Paul Boddington Apr 01 '16 at 20:14
  • the marked duplicate DOES NOT answer, I tried what is suggested and I get ArrayStoreException. post updated – brain storm Apr 01 '16 at 20:26
  • @Gremash: would you mind posting your solution in comment here. – brain storm Apr 01 '16 at 20:26
  • 1
    @Tunaki: it is not stream conversion. It is object casting. my input is Integer and output should be string. – brain storm Apr 01 '16 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. – Paul Boddington Apr 01 '16 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)`. – Tunaki Apr 01 '16 at 20:30
  • 1
    Not a dupe, although the answers to both involve streams. – Mark Harrison Apr 01 '16 at 20:56

0 Answers0