I had the following piece of code that IntelliJ suggested I should change:
String[] normalizedNames = rawToNormalized.values().stream().toArray(String[]::new);
into
String[] normalizedAliases = rawToNormalized.values().toArray(new String[0]);
Aleksey Shipilёv's post (https://shipilev.net/blog/2016/arrays-wisdom-ancients/) suggested:
toArray(new T[0]) seems faster, safer, and contractually cleaner, and therefore should be the default choice now."
Can someone please elaborate how exactly toArray(new String[0])
differs from toArray(String[]::new)
by going from stream and using toArray
without Collector
.
Can toArray(new String[0])
still be used in the stream? Will it still be a more performant/better choice?
I would follow intelliJ as one simple reason :
Why you trasform your list to stream to create an array. In the other side you can transform that list to an array directly.
Can someone please elaborate how exactly toArray(new String[0]) differs from toArray(String[]::new) and if toArray(new String[0]) still the more performant/better choice?
The idea behind the intelliJ warning is about using stream befor toArray and not the way of using toArray
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