Java 8 forEach with Map Example

In this article we are going to see about Java 8 forEach on map example program

ForEachMap.java

package com.ehowtonow.java8;

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

public class ForEachMap {

 public static void main(String[] args) {
  Map<String, Integer> batsmanRanking = new HashMap<String, Integer>();
  batsmanRanking.put("Virat Kohli", 1);
  batsmanRanking.put("AB de Villiers", 2);
  batsmanRanking.put("David Warner", 3);
  batsmanRanking.put("Joe Root", 4);
  batsmanRanking.put("Kane Williamson", 5);
  batsmanRanking.put("Quinton de Kock", 6);
  batsmanRanking.put("Faf du Plessis", 7);
  batsmanRanking.put("Babar Azam", 8);
  batsmanRanking.put("Martin Guptill", 9);
  batsmanRanking.put("Shikhar Dhawan", 10);

  System.out.println("ICC One day Batsman Ranking");
  //Normal For each
  for (Map.Entry entry : batsmanRanking.entrySet()) {
   System.out.println("Name : " + entry.getKey() + " Rank : " + entry.getValue());
  }
  
  //Java 8 For Each
  System.out.println("Java 8 forEach ICC One day Batsman Ranking");
  batsmanRanking.forEach((k,v)->System.out.println("Name : " + k + " Rank : " + v));

  System.out.println("Java 8 forEach with condtion ICC One day Batsman Ranking ");
  
  batsmanRanking.forEach((k,v)->{
   
   if("AB de Villiers".equals(k)){
    System.out.println("Hello AB de Villiers");
   }
  });
 }

}

Output :
ICC One day Batsman Ranking
Name : Faf du Plessis Rank : 7
Name : Virat Kohli Rank : 1
Name : Martin Guptill Rank : 9
Name : Babar Azam Rank : 8
Name : Quinton de Kock Rank : 6
Name : Kane Williamson Rank : 5
Name : AB de Villiers Rank : 2
Name : Joe Root Rank : 4
Name : David Warner Rank : 3
Name : Shikhar Dhawan Rank : 10
Java 8 forEach ICC One day Batsman Ranking
Name : Faf du Plessis Rank : 7
Name : Virat Kohli Rank : 1
Name : Martin Guptill Rank : 9
Name : Babar Azam Rank : 8
Name : Quinton de Kock Rank : 6
Name : Kane Williamson Rank : 5
Name : AB de Villiers Rank : 2
Name : Joe Root Rank : 4
Name : David Warner Rank : 3
Name : Shikhar Dhawan Rank : 10
Java 8 forEach with condtion ICC One day Batsman Ranking
Hello 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 convert List to Map with Duplicate Keys example
  2. Java – BiPredicate Interface
  3. Java – LongUnaryOperator Interface
  4. Java 8 Stream Filter with map Example
  5. Java – LongStream Interface
  6. Java 8 forEach with List example
  7. Java – ToIntFunction Interface
  8. Java – StreamSupport Class
  9. Java 8 – filter the null values from Stream Example
  10. Java – IntConsumer Interface
  11. Java – ToDoubleFunction Interface
  12. Java 8 New Features Overview
  13. Java 8 Stream Collectors groupingBy Example
  14. Java 8 Stream has already been operated upon or closed
  15. Java 8 forEach with Map Example

Be the first to comment

Leave a Reply