还有第三种选择-使用stream().toArray()
-请参见为什么流没有toList方法下的注释。结果比forEach()或collect()慢,表达力也较差。它可能会在以后的JDK版本中进行优化,因此在这里添加只是以防万一。
假设List<String>
myFinalList = Arrays.asList(
myListToParse.stream()
.filter(Objects::nonNull)
.map(this::doSomething)
.toArray(String[]::new)
);
使用微型基准测试,1M条目,20%为空值,并在doSomething()中进行简单转换
private LongSummaryStatistics benchmark(final String testName, final Runnable methodToTest, int samples) {
long[] timing = new long[samples];
for (int i = 0; i < samples; i++) {
long start = System.currentTimeMillis();
methodToTest.run();
timing[i] = System.currentTimeMillis() - start;
}
final LongSummaryStatistics stats = Arrays.stream(timing).summaryStatistics();
System.out.println(testName + ": " + stats);
return stats;
}
结果为
并行:
toArray: LongSummaryStatistics{count=10, sum=3721, min=321, average=372,100000, max=535}
forEach: LongSummaryStatistics{count=10, sum=3502, min=249, average=350,200000, max=389}
collect: LongSummaryStatistics{count=10, sum=3325, min=265, average=332,500000, max=368}
连续的:
toArray: LongSummaryStatistics{count=10, sum=5493, min=517, average=549,300000, max=569}
forEach: LongSummaryStatistics{count=10, sum=5316, min=427, average=531,600000, max=571}
collect: LongSummaryStatistics{count=10, sum=5380, min=444, average=538,000000, max=557}
并行操作时无需考虑空值和过滤(以便流是SIZED
):在这种情况下,toArrays
具有最佳性能,而.forEach()
会在接收方ArrayList上失败并出现“indexOutOfBounds”错误,必须替换为.forEachOrdered()
toArray: LongSummaryStatistics{count=100, sum=75566, min=707, average=755,660000, max=1107}
forEach: LongSummaryStatistics{count=100, sum=115802, min=992, average=1158,020000, max=1254}
collect: LongSummaryStatistics{count=100, sum=88415, min=732, average=884,150000, max=1014}
elt -> elt != null
可以替换为Objects::nonNull
。 - the8472Optional<T>
和flatMap
。 - hermandoSomething
是非静态方法,则可以编写.map(this::doSomething)
。如果它是静态的,则可以将this
替换为类名。 - herman