I was following this Question and was trying to get a new way of concatenating two arrays.
int[] c = {1, 34};
int[] d = {3, 1, 5};
I came up with this:
Integer [] res= Stream.of(c, d)
.flatMap(Stream::of)
.toArray(Integer[]::new);
Above compiles fine but i get this exception:
Exception in thread "main" java.lang.ArrayStoreException
at java.lang.System.arraycopy(Native Method)
at java.util.stream.SpinedBuffer.copyInto(SpinedBuffer.java:194)
at java.util.stream.Nodes$SpinedNodeBuilder.copyInto(Nodes.java:1290)
at java.util.stream.SpinedBuffer.asArray(SpinedBuffer.java:215)
at java.util.stream.Nodes$SpinedNodeBuilder.asArray(Nodes.java:1296)
at java.util.stream.ReferencePipeline.toArray(ReferencePipeline.java:439)
What is my mistake, and please give a explanation so that i can understand the concept.
PS: I have seen Dealing with an ArrayStoreException and believe that my question consists of stream which are incompitable types that is why they are not getting stored, so two questions are different.