How to Convert a Java 8 Stream to an Array? -
what easiest/shortest way convert java 8 stream array?
the easiest method use toarray(intfunction<a[]> generator) method array constructor reference. suggested in api documentation method.
string[] stringarray = streamstring.toarray(string[]::new); what does, find method takes in integer (the size) argument, , returns string[], (one of overloads of) new string[] does.
you write own intfunction:
stream<string> stream = ...; string[] stringarray = stream.toarray(size -> new string[size]); the purpose of intfunction<a[]> generator convert integer, size of array, new array.
example code:
stream<string> streamstring = stream.of("a", "b", "c"); string[] stringarray = streamstring.toarray(size -> new string[size]); arrays.stream(stringarray).foreach(system.out::println); prints:
a b c
Comments
Post a Comment