哪种 toArray 转换模式最适合使用?
Which toArray conversion pattern is best to use?
我有以下 IntelliJ 建议我应该更改的代码:
String[] normalizedNames = rawToNormalized.values().stream().toArray(String[]::new);
进入
String[] normalizedAliases = rawToNormalized.values().toArray(new String[0]);
Aleksey Shipilёv 的 post (https://shipilev.net/blog/2016/arrays-wisdom-ancients/) 建议:
toArray(new T[0]) seems faster, safer, and contractually cleaner, and therefore should be the default choice now."
有人可以详细说明 toArray(new String[0])
与 toArray(String[]::new)
有何不同,方法是从流和使用 toArray
而不使用 Collector
。
直播中还能使用toArray(new String[0])
吗?它仍然是一个更 performant/better 的选择吗?
我会跟随 intelliJ 的一个简单原因:
为什么要将列表转换为流来创建数组。在另一边,您可以将该列表直接转换为数组。
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?
intelliJ 警告背后的想法是关于在 toArray 之前使用流,而不是使用 toArray 的方式
我有以下 IntelliJ 建议我应该更改的代码:
String[] normalizedNames = rawToNormalized.values().stream().toArray(String[]::new);
进入
String[] normalizedAliases = rawToNormalized.values().toArray(new String[0]);
Aleksey Shipilёv 的 post (https://shipilev.net/blog/2016/arrays-wisdom-ancients/) 建议:
toArray(new T[0]) seems faster, safer, and contractually cleaner, and therefore should be the default choice now."
有人可以详细说明 toArray(new String[0])
与 toArray(String[]::new)
有何不同,方法是从流和使用 toArray
而不使用 Collector
。
直播中还能使用toArray(new String[0])
吗?它仍然是一个更 performant/better 的选择吗?
我会跟随 intelliJ 的一个简单原因:
为什么要将列表转换为流来创建数组。在另一边,您可以将该列表直接转换为数组。
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?
intelliJ 警告背后的想法是关于在 toArray 之前使用流,而不是使用 toArray 的方式