Is there a way to use streams to write this code:
for (int i = 0; i < list.size(); i ++) {
if (i % 1000 == 0) {
doSomething();
}
doSomethingElse(list.get(i));
}
Thanks!
Is there a way to use streams to write this code:
for (int i = 0; i < list.size(); i ++) {
if (i % 1000 == 0) {
doSomething();
}
doSomethingElse(list.get(i));
}
Thanks!
You could use an IntStream
for that... but why should you? It looks basically the same as what you wrote, but has some overhead due to the IntStream
which is not really needed here.
IntStream.range(0, list.size())
.forEach(i -> {
if (i % 1000 == 0) {
doSomething();
}
doSomethingElse(list.get(i));
});
Without knowing what doSomething
or doSomethingElse
do, it's hard to make a better proposal. Maybe you want to (or should?) partition your list beforehand?