What's best for read-iterating over an IntArrayList
from the fastutil library in Java?
fastutil is a library that is used for increased performance above what the standard library's collection classes and algorithms can do. So "best" in this context means best performance.
I've followed the advice in the fastutil docs and set my IDE (Eclipse) to warn when boxing happens:
Window -> Preferences -> Java -> Compiler -> Errors/Warnings -> Potential Programming Problems -> Boxing and unboxing conversions -> set to Warning
However, Eclipse seems to omit some warnings. See below in the code for details.
I came across these alternatives so far for iterating:
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()
}