How to use forEach in Java 8?

Here is a some Example to explain how to loop ForEach in java.

1- List with forEach

  • First example explain how to loop a List with forEach
package com.nourit.java8;

import java.util.Arrays;
import java.util.List;

public class ForEachExample1 {

  public static void main(String[] args) {
    List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
    System.out.println("-------------- Read a List With normal For --------------------");
    for(int item : list){
      System.out.println("The number : " +item);
    }
    
    System.out.println("-------------- Read a List With ForEach --------------------");
    list.forEach((Integer item) -> {
      System.out.println("The number : " + item);
    });
    System.out.println("--------------------------------------------");
  }
}

Output:

-------------- Read a List With normal For --------------------
The number : 1
The number : 2
The number : 3
The number : 4
The number : 5
The number : 6
The number : 7
The number : 8
The number : 9
-------------- Read a List With ForEach --------------------
The number : 1
The number : 2
The number : 3
The number : 4
The number : 5
The number : 6
The number : 7
The number : 8
The number : 9
--------------------------------------------

 

  • Second example explain how to loop a List with forEach and filter
package com.nourit.java8;

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

public class ForEachExample2 {

  public static void main(String[] args) {
    List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
    System.out.println("-------------- Read a List With normal For --------------------");
    List<Integer> filteredList = new ArrayList<Integer>();
    for (int i : list) {
      if (i % 3 == 0) {
        filteredList.add(i);
      }
    }
    System.out.println("The List filtered with for:     " + filteredList);
    System.out.println("-------------- Read a List With ForEach --------------------");
    List<Integer> filteredListNew = new ArrayList<Integer>();
    list.forEach(n -> {
      if (n % 3 == 0) {
        filteredListNew.add(n);
      }
    });
    System.out.println("The List filtered with forEach:   " + filteredListNew);
    System.out.println("--------------------------------------------");

  }
}

Output:

-------------- Read a List With normal For --------------------
The List filtered with for:     [3, 6, 9]
-------------- Read a List With ForEach --------------------
The List filtered with forEach:   [3, 6, 9]
--------------------------------------------
  • Third example with method reference
package com.nourit.java8;

import java.util.Arrays;
import java.util.List;

public class ForEachExample3 {

  public static void main(String[] args) {
    List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
    System.out.println("-------------- Read a List With normal For --------------------");
    for(int item : list){
      System.out.println(item);
    }
    
    System.out.println("-------------- Read a List With ForEach --------------------");
    list.forEach(System.out::println);
    System.out.println("--------------------------------------------");

  }
}

Output:

-------------- Read a List With normal For --------------------
1
2
3
4
5
6
7
8
9
-------------- Read a List With ForEach --------------------
1
2
3
4
5
6
7
8
9
--------------------------------------------

2- Map with forEach

here an example explain how to loop a Map with forEach and lambda expression.

package com.nourit.java8;

import java.util.HashMap;
import java.util.Map;

public class ForEachMapExample {

  public static Map<String, String> map = new HashMap<String, String>() {
    private static final long serialVersionUID = 1L;
    {
      put("CAT", "MyawMyaw");
      put("DOG", "HawHaw");
      put("DUCK", "WakWak");
    }
  };

  public static void main(String[] args) {
    // we use entrySet() to get both the key and value
    System.out.println("------------- simple loop ---------------------");
    for (Map.Entry<String, String> entry : map.entrySet()) {
      System.out.println("The " + entry.getKey() + " say : " + entry.getValue());
    }

    System.out.println("------------- ForEach loop ---------------------");
    map.forEach((k, v) -> System.out.println("The " + k + " say : " + v));
    
    System.out.println("------------- ForEach loop with Java 8 Stream Api ---------------------");
    map.entrySet().stream().forEach(e -> System.out.println("The " + e.getKey() + " say : " + e.getValue()));
    
    System.out.println("------------- ForEach loop with Java 8 Stream Api parallel ---------------------");
    map.entrySet().stream().parallel().forEach(e -> System.out.println("The " + e.getKey() + " say : " + e.getValue()));
    System.out.println("------------- ------------- -------------------------------");
  }
}

Output:

------------- simple loop ---------------------
The CAT say : MyawMyaw
The DUCK say : WakWak
The DOG say : HawHaw
------------- ForEach loop ---------------------
The CAT say : MyawMyaw
The DUCK say : WakWak
The DOG say : HawHaw
------------- ForEach loop with Java 8 Stream Api ---------------------
The CAT say : MyawMyaw
The DUCK say : WakWak
The DOG say : HawHaw
------------- ForEach loop with Java 8 Stream Api parallel ---------------------
The DUCK say : WakWak
The CAT say : MyawMyaw
The DOG say : HawHaw
------------- ------------- -------------------------------

 

NOUR-IT

IT Training Center for Java and Web technology. If you enjoy and like our tutorials and believe in our mission, Please Subscribe us on Youtube and follow us on Twitter, or befriend us on Facebook or Google Plus.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

*