一、在调用Set的toArray()方法的时候
在使用HashMap的时候,它的keySet()
方法将返回一个Set集合
且Set是继承自Collection的,在使用toArray()的时候其实转换后的数组类型是Object[]
如果直接使用String[]去接会报错:
String[] keys = articleMap.keySet().toArray();
所以这里使用官方给出的例子,加上想要转的new数组new String[0]
就好了:
String[] keys = articleMap.keySet().toArray(new String[0]);
二、零大小的(collection.toArray(new T[0]))和预尺寸的(collection.toArray(new T[size])变体性能比较
两种方法获得结果都一样,但是collection.toArray(new T[0])性能会更好一些。
Java 语言规范指示新实例化的数组和对象应具有缺省字段值,而不是内存中的不规则剩余部分。因此,运行时必须将预分配的存储清零
。基准测试实验已经证明,零大小的数组方法调用设法避免了清零,但预先确定大小的案例却不能。
所以零大小的数组方法运行速度更快,性能要更好。
在较新的 JDK 上进行基准测试
# VM version: JDK 11.0.2, OpenJDK 64-Bit Server VM, 11.0.2+9
-----------------------------------------------------------------------------------
Benchmark (size) (type) Mode Cnt Score Error Units
-----------------------------------------------------------------------------------
ToArrayBenchmark.zero_sized 100 array-list avgt 15 199.920 ± 11.309 ns/op
ToArrayBenchmark.pre_sized 100 array-list avgt 15 237.342 ± 14.166 ns/op
-----------------------------------------------------------------------------------
ToArrayBenchmark.zero_sized 100 tree-set avgt 15 819.306 ± 85.916 ns/op
ToArrayBenchmark.pre_sized 100 tree-set avgt 15 972.771 ± 69.743 ns/op
###################################################################################
# VM version: JDK 14.0.2, OpenJDK 64-Bit Server VM, 14.0.2+12-46
------------------------------------------------------------------------------------
Benchmark (size) (type) Mode Cnt Score Error Units
------------------------------------------------------------------------------------
ToArrayBenchmark.zero_sized 100 array-list avgt 15 158.344 ± 3.862 ns/op
ToArrayBenchmark.pre_sized 100 array-list avgt 15 214.340 ± 5.877 ns/op
------------------------------------------------------------------------------------
ToArrayBenchmark.zero_sized 100 tree-set avgt 15 877.289 ± 132.673 ns/op
ToArrayBenchmark.pre_sized 100 tree-set avgt 15 934.550 ± 148.660 ns/op
####################################################################################
# VM version: JDK 15.0.2, OpenJDK 64-Bit Server VM, 15.0.2+7-27
------------------------------------------------------------------------------------
Benchmark (size) (type) Mode Cnt Score Error Units
------------------------------------------------------------------------------------
ToArrayBenchmark.zero_sized 100 array-list avgt 15 147.925 ± 3.968 ns/op
ToArrayBenchmark.pre_sized 100 array-list avgt 15 213.525 ± 6.378 ns/op
------------------------------------------------------------------------------------
ToArrayBenchmark.zero_sized 100 tree-set avgt 15 820.853 ± 105.491 ns/op
ToArrayBenchmark.pre_sized 100 tree-set avgt 15 947.433 ± 123.782 ns/op
####################################################################################
# VM version: JDK 16, OpenJDK 64-Bit Server VM, 16+36-2231
------------------------------------------------------------------------------------
Benchmark (size) (type) Mode Cnt Score Error Units
------------------------------------------------------------------------------------
ToArrayBenchmark.zero_sized 100 array-list avgt 15 146.431 ± 2.639 ns/op
ToArrayBenchmark.pre_sized 100 array-list avgt 15 214.117 ± 3.679 ns/op
------------------------------------------------------------------------------------
ToArrayBenchmark.zero_sized 100 tree-set avgt 15 818.370 ± 104.643 ns/op
ToArrayBenchmark.pre_sized 100 tree-set avgt 15 964.072 ± 142.008 ns/op
####################################################################################
零大小的collection.toArray(new T[0])方法一直比预定尺寸的collection.toArray(new T[size])快。此外,它的性能随着JDK的每个新版本而不断提高。
参考:Java中collection.toArray(new String[0])的语法解释
Collection.toArray(new T[0]) or .toArray(new T[size])