什么是对一个基元ArrayList进行读迭代的最佳做法?IntArrayList
来自 速冻库 在Java中?
fastutil是一个用于提高性能的库,其性能高于标准库的集合类和算法所能做到的。所以这里的 "最好 "就是指最好的性能。
我已经按照 在fastutil docs的建议 并设置我的IDE(Eclipse)在装箱发生时发出警告:Window -> Preferences -> Java -> Compiler -> ErrorsWarnings -> Potential Programming Problems -> Boxing and unboxing conversions -> set to Warning 然而,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()
}
绝对是第一条:)
[我还得再加一个StackOverflow不让我发的字符]。