Java 8 Iterable.forEach() vs foreach loop -


which of following better practice in java 8?

java 8:

joins.foreach(join -> mirc.join(msession, join)); 

java 7:

for (string join : joins) {     mirc.join(msession, join); } 

i have lots of loops "simplified" lambdas, there advantage of using them including performance , readability?

edit

i'll extend question longer methods - know cant return or break parent function lambda , should mentioned if compared, there else considered?

the advantage comes account when operations can executed in parallel. (see http://java.dzone.com/articles/devoxx-2012-java-8-lambda-and - section internal , external iteration)

  • the main advantage point of view implementation of done within loop can defined without having decide if executed in parallel or sequential

  • if want loop executed in parallel write

     joins.parallelstream().foreach((join) -> mirc.join(msession, join)); 

    you have write code thread handling etc.

note : answer assumed joins implementing java.util.stream interface. if joins implements java.util.iterable interface no longer true.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -