String[] arr={"121","4545","45456464"};
Arrays.stream(arr).filter(s->s.length()>4).toArray(String[]::new);
Can someone tell me exactly what's happening with toArray(String[]::new)
in above code snippet.
String[]::new
is actually the same as size -> new String[size]
.
A new String[]
is created with the same size as the number of elements after applying the filter
to the Stream
. See also the javadoc of Stream.toArray
The toArray
is creating a String[]
containing the result of the filter
in your case all strings whose length is greater than 4. The filter
returns a Stream
and so you are converting it into an array.
To print the filtered result rather than storing it into an array
Arrays.stream(arr).filter(s -> s.length() > 4).forEach(System.out::println);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With