Java 8 and Streams are really nice. They can even make look ordinary for loops much cooler. Assume the following snippet:
for (int i = 0; i < 10; i++) {
elements[i] = new MyModel(i);
}
This can also be written with the IntStream
class.
IntStream.range(0, 10).forEach(i -> elements[i] = new MyModel(i));
I think the second statement looks much cooler.
Anyone knows if the old for loop is more efficient? I would assume IntStream is handled well enough be the Java compiler but did not check.
16 Responses to Using IntStream.range instead of an ordinary for loop