The
inputString.split(" ") method returns an array of strings. This is same as:
String [] result = inputString.split(" ");
In the example, any of the two methods usage returns the same output. But, there are some differences in the two methods. Here are the definitions of the two stream methods and examples.
Version One:
@SafeVarargs
static <T> Stream<T> of(T... values)
Returns a sequential ordered stream whose elements are the specified values.
EXAMPLES:
Version Two:
static <T> Stream<T> stream(T[] array)
Returns a sequential Stream with the specified array as its source.
EXAMPLES:
Note that the method Arrays.Stream method is overloaded to take various primitive arrays or a type array to return streams as in the examples above. These methods are capable of returning primitive streams (e.g., IntStream). Arrays class also has variations of this method to select a range of elements from the input array to create the stream.
Both the method versions return sequential streams. But, the
Stream class's method input must be an object type as input, and the following code will not compile on line 2:
Also, note that in the first version the stream method allows varargs as its arguments. This allows elements, as shown in the above version one example, can be entered without creating an array explicitly.
And why did you choose one over another in your blog post?
I really don't recollect why I did that. I think it means same output in this case.
Thanks for reading the article.
SCJP 5, OCPJP 7, 8, SCJD 5, SCWCD 4, SCBCD 5, SCJWS 4, IBM OOAD 833 & 834, MongoDB Developer