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?