gpt4 book ai didi

java - 哪种 toArray 转换模式最适合使用?

转载 作者:行者123 更新时间:2023-11-30 05:25:59 25 4
gpt4 key购买 nike

IntelliJ 建议我更改以下代码:

String[] normalizedNames = rawToNormalized.values().stream().toArray(String[]::new);

进入

String[] normalizedAliases = rawToNormalized.values().toArray(new String[0]);

Aleksey Shipilёv 的帖子 ( 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]) 还能在流中使用吗?它仍然是一个性能更高/更好的选择吗?

最佳答案

我关注 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 的方式

关于java - 哪种 toArray 转换模式最适合使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58592819/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com