我有一个任务要在树集中添加 > 10000000 个序列元素。
如果我用
for (long index = 0; index < 10000000; index++)
{
result.add(index);
}
需要 8083 毫秒。是否有任何解决方案可以提高此任务的性能?
https://github.com/cyberterror/TestRanges
附言目前最快的方式是:List<Integer> range = IntStream.range(0, 10000000).boxed().collect(Collectors.toList());
结果为 ~ 370 毫秒
最佳答案
试用 HPPC:Java 的高性能原始集合
许可证:Apache 许可证 2.0
<dependency>
<groupId>com.carrotsearch</groupId>
<artifactId>hppc</artifactId>
<version>0.7.1</version>
</dependency>
LongHashSet 在 1190 毫秒内执行:
LongSet result = new LongHashSet();
for (Long index = 0L; index < 10000000L;) {
result.add(index++);
}
LongScatterSet 在 850 毫秒内执行:
LongSet result = new LongScatterSet();
for (Long index = 0L; index < 10000000L;) {
result.add(index++);
}
关于java - set集合中添加元素最快的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37411625/