I tested an array with 10 million elements and iterating is always faster than streams. Can someone explains why?
String[] words = new String[10000000];
Arrays.fill(words, "hello world!");
Instant start = Instant.now();
long count = 0;
for (String word : words) {
if (word.length() > 8)
count++;
}
Instant end = Instant.now();
System.out.println("Using iterations:" + count + " in " + Duration.between(start, end).toMillis());
start = Instant.now();
count = Arrays.stream(words).parallel().filter(word -> word.length() > 8).count();
end = Instant.now();
System.out.println("Using Streams :" + count + " in " + Duration.between(start, end).toMillis());
Sample run
Using iterations:10000000 in 13
Using Streams :10000000 in 53