IntArrayList
什么最适合从 Java 中的fastutil 库中读取迭代?
fastutil 是一个库,用于提高标准库的集合类和算法所能做的性能。因此,在这种情况下,“最佳”意味着最佳性能。
我遵循了fastutil 文档中的建议,并将我的 IDE(Eclipse)设置为在发生装箱时发出警告:Window -> Preferences -> Java -> Compiler -> Errors/Warnings -> Potential Programming Problems -> Boxing and unboxing conversions - > 设置为警告
然而,Eclipse 似乎忽略了一些警告。有关详细信息,请参见下面的代码。
到目前为止,我遇到了这些替代方案进行迭代:
IntArrayList list = ...;
// 1. for-loop-with-index-variable
for (int i = 0; i < list.size(); i++) {
// do something with list.getInt(i)
}
// 2. for-each-loop-with-wrapped
Integer value = list.getInt(0); // Eclipse correctly warns in this line
for (Integer element : list) { // autoboxing happens here somewhere, but Eclipse does NOT warn
// do something with element
}
// 3. for-each-loop-with-primitive
for (int element : list) { // Eclipse does NOT warn. Does autoboxing happen here?
// do something with element
}
// 4. forEach-method-with-consumer
list.forEach((int element) -> {
// do something with element
});
// 5. for-loop-with-IntIterator
for (IntIterator iter = list.iterator(); iter.hasNext();) {
// do something with iter.nextInt()
}