How many ways to iterate in HashMap ?


Q How many ways to iterate in HashMap ?

Ans : five ( 5) ways


 package com.gsr.Map;

import java.util.HashMap;

import java.util.Iterator;

import java.util.Map;

import java.util.Map.Entry;


public class IteratorWaysInMAP {

public static void IteratorUsingEntrySet(Map<Integer,String> courseMap) {

Iterator<Entry<Integer,String>> iterator=courseMap.entrySet().iterator();

System.out.println("Iterator Using EntrySet");

while(iterator.hasNext()) {

Entry<Integer,String> entry=iterator.next();

System.out.println("Key is : "+entry.getKey()+"  and value is "+entry.getValue());

}

}

public static void IteratorUsingKeySet(Map<Integer,String> courseMap) {

Iterator<Integer> iterator=courseMap.keySet().iterator();

System.out.println("Iterator Using KeySet");

while(iterator.hasNext()) {

Integer key=iterator.next();

System.out.println("Key is : " +key+" and value is  "+courseMap.get(key));

}

}

public static void IteratorUsingFOREACH(Map<Integer, String> courseMap) {

System.out.println("Iterator Using For Each Loop");

for(Entry<Integer,String> entry: courseMap.entrySet()) {

System.out.println("Key is : "+entry.getKey()+" and value is : "+entry.getValue());

}

}

public static void IteratorUsingLAMBDAEXPRESSION(Map<Integer, String> courseMap) {

System.out.println("Iterator Using Lambda Expression ");

courseMap.forEach((key,value)->{

System.out.println("Key is : "+key+" and value is : "+value);

});

}

public static void IteratorUsingSTREAMAPI(Map<Integer, String> courseMap) {

System.out.println("Iterator Using Stream API ");

courseMap.entrySet().stream().forEach((entry)->{

System.out.println("Key is : "+entry.getKey()+" and value is : "+entry.getValue());

});

}

public static void main(String[] args) {

Map<Integer, String> courseMap=new HashMap<Integer,String>();

courseMap.put(1, "Hindi");

courseMap.put(2, "English");

courseMap.put(3, "Social Science");

courseMap.put(4, "Science");

courseMap.put(5, "Mathematics");

courseMap.put(6, "Computer Science");

courseMap.put(7, "Moral Science");

IteratorWaysInMAP.IteratorUsingKeySet(courseMap);

IteratorWaysInMAP.IteratorUsingEntrySet(courseMap);

IteratorWaysInMAP.IteratorUsingFOREACH(courseMap);

IteratorWaysInMAP.IteratorUsingLAMBDAEXPRESSION(courseMap);

IteratorWaysInMAP.IteratorUsingSTREAMAPI(courseMap);

}

}


Output :

Iterator Using KeySet

Key is : 1 and value is  Hindi

Key is : 2 and value is  English

Key is : 3 and value is  Social Science

Key is : 4 and value is  Science

Key is : 5 and value is  Mathematics

Key is : 6 and value is  Computer Science

Key is : 7 and value is  Moral Science

Iterator Using EntrySet

Key is : 1  and value is Hindi

Key is : 2  and value is English

Key is : 3  and value is Social Science

Key is : 4  and value is Science

Key is : 5  and value is Mathematics

Key is : 6  and value is Computer Science

Key is : 7  and value is Moral Science

Iterator Using For Each Loop

Key is : 1 and value is : Hindi

Key is : 2 and value is : English

Key is : 3 and value is : Social Science

Key is : 4 and value is : Science

Key is : 5 and value is : Mathematics

Key is : 6 and value is : Computer Science

Key is : 7 and value is : Moral Science

Iterator Using Lambda Expression 

Key is : 1 and value is : Hindi

Key is : 2 and value is : English

Key is : 3 and value is : Social Science

Key is : 4 and value is : Science

Key is : 5 and value is : Mathematics

Key is : 6 and value is : Computer Science

Key is : 7 and value is : Moral Science

Iterator Using Stream API 

Key is : 1 and value is : Hindi

Key is : 2 and value is : English

Key is : 3 and value is : Social Science

Key is : 4 and value is : Science

Key is : 5 and value is : Mathematics

Key is : 6 and value is : Computer Science

Key is : 7 and value is : Moral Science



Comments