For, foreach.

Discussion in 'Plugin Development' started by JanTuck, Jan 22, 2017.

Thread Status:
Not open for further replies.
  1. Offline

    JanTuck

    So i normally do for example.

    Code:java
    1.  
    2. Collection<HashSet<Block>> blockSets = chunkSplitter.values();
    3.  


    I would do something like

    Code:java
    1.  
    2. for (HashSet<Block> : blockSets)
    3. {
    4. // Do something with the hashset.
    5. }
    6.  


    But i recently came aware of .foreach()

    Code:java
    1.  
    2. blockSets.forEach(blocks -> {
    3. // Do something with the hashset
    4. });
    5.  



    What is the difference?
     
  2. Offline

    timtower Administrator Administrator Moderator

    @JanTuck That foreach is a java 8+ feature I believe.
     
  3. @JanTuck
    There really is no difference, one is a for loop, one is a lambda expression (new in Java 8).
     
  4. Offline

    JanTuck

  5. Offline

    mythbusterma

    @JanTuck

    Well one also does streaming and uses Java 8 only features. It can also use implicit parallelization.
     
  6. Offline

    JanTuck

    @mythbusterma
    I prefer .foreach() i just wanted to know what is best to use.
     
  7. @JanTuck If you are supporting servers on Java 7, the regular for loop, if you are mainly for Java 8 you can use the forEach it does in my opinion also look nicer and can take a lot less code.
     
    JanTuck likes this.
Thread Status:
Not open for further replies.

Share This Page