Menu Bar

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Thursday, 9 February 2017

What are Different ways to Iterate Over HashMap in Java ?

There are many ways to iterate Map interface classes (HashMap, HashTable, LinkedHashMap and TreeMap) ,here with will traverse map with the help of for-each loop , Iterator interface and Lambda Expression
We can iterate only keys of map using keySet() method, we can iterate only values of map using values() method, and if we want to iterate keys and values of map simultaneously then we can use entrySet() method. See below example for detail.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

public class WaysToIterateHashMap {
 
 public static void main(String[] args) {
  HashMap<String, String> map = new HashMap<>();
  map.put("UP", "UTTA PPRADESH");
  map.put("GA", "GOA");
  map.put("CH", "CHANDIGARH");
  map.put("DL", "DELHI");
  map.put("WB", "WEST BENGAL");
  
  System.out.println("Using For-Each with keySet() :-");
  for(String s : map.keySet()){
   System.out.println(s+" = "+map.get(s));
  }
  
  System.out.println("\nUsing ForEach with entrySet() :-");
  for(Entry<String, String> en : map.entrySet()){
   System.out.println(en.getKey()+" = "+en.getValue());
  }
  
  System.out.println("\nUsing Iterator with keySet() :-");
  Iterator<String> it = map.keySet().iterator();
  while(it.hasNext()){
   String key = it.next();
   System.out.println(key+" = "+map.get(key));
  }
  
  System.out.println("\nUsing Iterator with entrySet() :-");
  Iterator<Entry<String, String>> it1 = map.entrySet().iterator();
  while(it1.hasNext()){
   Entry<String, String> en = it1.next();
   System.out.println(en.getKey()+" = "+en.getValue());
  }
  
  System.out.println("\nUsing Lambda Expression :-");
  map.forEach((k,v) -> System.out.println(k+" = "+v));
 }
}

Output:

Using For-Each with keySet() :-
CH = CHANDIGARH
DL = DELHI
GA = GOA
UP = UTTA PPRADESH
WB = WEST BENGAL

Using ForEach with entrySet() :-
CH = CHANDIGARH
DL = DELHI
GA = GOA
UP = UTTA PPRADESH
WB = WEST BENGAL

Using Iterator with keySet() :-
CH = CHANDIGARH
DL = DELHI
GA = GOA
UP = UTTA PPRADESH
WB = WEST BENGAL

Using Iterator with entrySet() :-
CH = CHANDIGARH
DL = DELHI
GA = GOA
UP = UTTA PPRADESH
WB = WEST BENGAL

Using Lambda Expression :-
CH = CHANDIGARH
DL = DELHI
GA = GOA
UP = UTTA PPRADESH
WB = WEST BENGAL

      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Share this Blog with yours Friends !!

No comments:

Post a Comment