For Each loop of Java 8 ..!!

For Each loop of Java 8 ..!!

The main advantage of using the forEach() method is when it is invoked on a parallel stream, in that case we don't need to wrote code to execute in parallel.

Therefore, whenever parallel execution could possibly improve the performance of the program, the forEach() method should be considered as a good option.

forEach() is useful for the following:

  •  Atomically iterating over a synchronized list. Prior to this, a list generated with Collections.synchronizedList() was atomic with respect to things like get or set, but was not thread-safe when iterating.
  •  Parallel execution (using an appropriate parallel stream). This saves you a few lines of code vs using an ExecutorService, if your problem matches the performance assumptions built into Streams and Spliterators.
  •  Specific containers which, like the synchronized list, benefit from being in control of iteration (although this is largely theoretical unless people can bring up more examples)
  •  Calling a single function more cleanly by using forEach() and a method reference argument (ie, list.forEach (obj::someMethod)). However, keep in mind the points on checked exceptions, more difficult debugging, and reducing the number of idioms you use when writing code.

Just a small basic demo I have shown for this..



To view or add a comment, sign in

More articles by 🧿 🟨Saral Saxena 🧑‍💻🏆

  • Run .http Files in a Intellij idea

    When developing APIs in a Spring Boot project, testing endpoints is a crucial part of the workflow. Tools like Postman…

  • Mastering API Versioning in Spring Boot: Stop Breaking Your Endpoints

    Introduction API versioning is one of the most overlooked yet critical aspects of API development. Many developers…

  • Blazing Fast Performance in Spring Boot 3 with CDS

    Spring Boot 3 brings several performance optimizations, but one of the most exciting ones is Class Data Sharing (CDS)…

  • Java 21 Docker Optimization

    Java 21 introduces significant improvements and features, particularly in containerization. This article explores five…

  • Java Optimization: 7000ms → 90ms

    When working with two separate lists and trying to match data based on a common id, many developers often default to…

  • Spring Boot Red Flags

    Spring Boot is a powerful framework designed to simplify Java application development by offering production-ready…

  • Stop Writing != null

    Null pointer exceptions are a familiar pain point in Java development. The go-to solution for many developers? Add a…

  • Spring boot Apps getting optimized

    Before making any changes, established clear performance baselines. Here’s what our initial metrics looked like:…

  • Validating Payloads with Spring Boot 3.4.0

    First, let’s examine a controller that receives a object. This object contains fields such as: first name, last name…

  • Limitations of Java Executor Framework.

    The Java Executor Framework has inherent limitations that affect its performance in high-throughput, low-latency…

Insights from the community

Others also viewed

Explore topics