I am just reading through some Java 8 code and from what I understand, you are able to iterate over collections using Stream() or parallelStream(). The latter has the advantage of using concurrency to split the task up over modern multicore processors and speed up the iteration (although it does not guarantee the order of the results).
The example code from the Oracle Java tutorial:
double average = roster
.stream() /** non-parallel **/
.filter(p -> p.getGender() == Person.Sex.MALE)
.mapToInt(Person::getAge)
.average()
.getAsDouble();
double average = roster
.parallelStream()
.filter(p -> p.getGender() == Person.Sex.MALE)
.mapToInt(Person::getAge)
.average()
.getAsDouble();
If I had a collection and I did not care about the order that it was processed in (i.e. they all have unique ID's or are unordered anyway or in a presorted state), would it make sense to always use the parallelStream way of iterating over a collection?
Other than when my code is run on a single core machine (at which point I assume the JVM would allocate all the work to the singlecore, thus not breaking my program), are there any drawbacks to using parallelStream() everywhere?