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

Popular posts from this blog

Why can rails not find a route created by a helper? -

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -