The for each loop

The for each loop

What is a forEach loop?

The syntaxic sugar to iterate over an iterable, internally the compiler creates an iterator for you and use it to traverse the iterable.


This syntax works for any iterable that includes all arrays and collection.


This iterator object is not an overhead, the JVM can quickly remove it through its escape analysis optimization. No one can get a reference to this iterator.?


If your object is already an iterator, you can use its forEach method and pass your consumer to it. The forEach() method of an iterable uses forEach loop internally.


You can support my work by subscribing to my YouTube channel below.

https://lnkd.in/dV3zYhTM

要查看或添加评论,请登录

Mothusi Molorane的更多文章

  • Delegate pattern, mixed feelings?

    Delegate pattern, mixed feelings?

    Using OpenApi code generator is a common practice as it allows developers to focus on writing business logic and let a…

  • Sleep vs Wait

    Sleep vs Wait

    In Java, sleep and wait are both used to pause the execution of a thread, but they serve different purposes and have…

    1 条评论
  • Deciphering Spring Boot magic

    Deciphering Spring Boot magic

    What is spring auto configuration? First, this question seems to be easy because as you know, most developers use…

  • Spring Dependency Injection

    Spring Dependency Injection

    Dependency Injection Is an action of supplying dependencies to a dependant bean. Since most of my audience are familiar…

  • MicroService All In One

    MicroService All In One

    Good morning to my LinkedIn friend. Today, I want to share my presentation slides again.

  • Spring Authorisation Server

    Spring Authorisation Server

    SPRING SECURITY “Spring Security is a framework that provides authentication, authorization, and protection against…

  • Spring Data Series 2

    Spring Data Series 2

    To my LinkedIn audience, happy Tuesday morning. Remember the list of topics we are looking at in our series.

  • JPA and Spring Data

    JPA and Spring Data

    I have already published several articles on spring data and I will be building from them. If you have not read the…

  • I had an interview with a developer

    I had an interview with a developer

    Mothusi: Hello guys, today I have a Java interview with JavaSpace (aka JS) the name of my YouTube channel. Please help…

    3 条评论
  • Answer to puzzle 3

    Answer to puzzle 3

    Remember last week I posted this code snippet below. This code seem like it will loop from 123456789 to 0 however it's…

社区洞察

其他会员也浏览了