Java 8 forEach with List example

In this article we are going to see about Java 8 forEach with List example program

ForEachList.java

package com.ehowtonow.java8;

import java.util.ArrayList;
import java.util.List;

public class ForEachList {

 public static void main(String[] args) {

  List batsmanRanking = new ArrayList<>();
  batsmanRanking.add("Virat Kohli");
  batsmanRanking.add("AB de Villiers");
  batsmanRanking.add("David Warner");
  batsmanRanking.add("Joe Root");
  batsmanRanking.add("Kane Williamson");
  batsmanRanking.add("Quinton de Kock");
  batsmanRanking.add("Faf du Plessis");
  batsmanRanking.add("Babar Azam");
  batsmanRanking.add("Martin Guptill");
  batsmanRanking.add("Shikhar Dhawan");

  System.out.println("Java 7 forEach : ICC One day Batsman Ranking");

  // Java 7 for each
  for (String batsman : batsmanRanking) {
   System.out.println("Name : " + batsman);

  }

  System.out.println("Java 8 forEach with lambda Expression : ICC One day Batsman Ranking");

  // lambda Expression
  batsmanRanking.forEach(item -> System.out.println(item));

  // Output : C
  batsmanRanking.forEach(item -> {
   if ("AB de Villiers".equals(item)) {
    System.out.println(item);
   }
  });

  System.out.println("Java 8 forEach with method reference : ICC One day Batsman Ranking");

  // method reference
  batsmanRanking.forEach(System.out::println);

  System.out.println("Java 8 forEach with Stream and filter : ICC One day Batsman Ranking");

  // Stream and filter
  batsmanRanking.stream().filter(s -> s.contains("AB de Villiers")).forEach(System.out::println);
 }

}

Output
Java 7 forEach : ICC One day Batsman Ranking
Name : Virat Kohli
Name : AB de Villiers
Name : David Warner
Name : Joe Root
Name : Kane Williamson
Name : Quinton de Kock
Name : Faf du Plessis
Name : Babar Azam
Name : Martin Guptill
Name : Shikhar Dhawan
Java 8 forEach with lambda Expression : ICC One day Batsman Ranking
Virat Kohli
AB de Villiers
David Warner
Joe Root
Kane Williamson
Quinton de Kock
Faf du Plessis
Babar Azam
Martin Guptill
Shikhar Dhawan
AB de Villiers
Java 8 forEach with method reference : ICC One day Batsman Ranking
Virat Kohli
AB de Villiers
David Warner
Joe Root
Kane Williamson
Quinton de Kock
Faf du Plessis
Babar Azam
Martin Guptill
Shikhar Dhawan
Java 8 forEach with Stream and filter : ICC One day Batsman Ranking
AB de Villiers


Ask your questions in eHowToNow Forum

Post your technical, non-technical doubts, questions in our site. Get answer as soon as possible, meanwhile you can help others by answering, unanswered questions.
To Ask new Question : Ask Question
Check our existing discussions : Questions & Answers

  1. Java 8 Stream Filter and Collect Example
  2. Java 8 convert List to Map example
  3. Java – ToDoubleFunction Interface
  4. Java – DoubleStream Interface
  5. Java – Consumer Interface
  6. Java 8 Overview and New Features
  7. Java – LongConsumer Interface
  8. Java – LongUnaryOperator Interface
  9. Java – Stream.Builder Interface
  10. Java – DoubleFunction Interface
  11. Java – BinaryOperator Interface
  12. Java – StreamSupport Class
  13. Java 8 forEach with List example
  14. Java – DoubleBinaryOperator Interface
  15. Java – LongStream Interface

Be the first to comment

Leave a Reply