Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to iterate or loop over HashMap (Map) in Java with Example

I have already shared how HashMap works internally in java. In this tutorial we will learn about the four different ways of looping or iterating over Map in java.

1. for-each loop
2. keyset() iterator
3. entrySet() and for loop
4. entrySet() and java iterator


1. Iterating or Looping Map Using foreach loop

In this we will use foreach loop to iterate over any map in java and using KeySet of the map to get keys. This will display keys and values together.


import java.util.*;

public class HashMapLoopExample {
public static void main(String args[]) {


// Creating a HashMap of String keys and String values
HashMapString, String> hashmap = new HashMapString, String>();
hashmap.put("Key1", "Value1");
hashmap.put("Key2", "Value2");
System.out.println("Iterating or Looping map using foreach loop");
// Iterating or looping using keySet() method
for (String key : hashmap.keySet()) {
System.out.println("key: " + key + " value: " + hashmap.get(key));
}
}
}


Output
Iterating or looping map using foreach loop
key: Key2 value: Value2
key: Key1 value: Value1



2. Iterating or looping Map Using keySet() iterator

In this example of looping HashMap in java we have used java iterator instead of for each loop.

import java.util.*;

public class HashMapLoopExample {
public static void main(String args[]) {

// Creating a HashMap of String keys and String values
HashMapString, String> hashmap = new HashMapString, String>();
hashmap.put("Key1", "Value1");
hashmap.put("Key2", "Value2");
System.out.println("Iterating or looping map using keySet Iterator");
// Iterating or looping using keySet() method
SetString> keySet = hashmap.keySet();
IteratorString> keySetIterator = keySet.iterator();
while (keySetIterator.hasNext()) {
String key = keySetIterator.next();
System.out.println("key: " + key + " value: " + hashmap.get(key));
}
}
}


Output
Iterating or looping map using keySet iterator
key: Key2 value: Value2
key: Key1 value: Value1

3. Iterating or Looping Map in Java using EntrySet and for each Loop



import java.util.*;

public class HashMapLoopExample {
public static void main(String args[]) {

// Creating a HashMap of String keys and String values
HashMapString, String> hashmap = new HashMapString, String>();
hashmap.put("Key1", "Value1");
hashmap.put("Key2", "Value2");
System.out.println("Iterating or looping map using entrySet and foreach loop");
// Iterating or looping using entrySet() method
SetMap.EntryString, String>> entrySet = hashmap.entrySet();
for (Map.Entry entry : entrySet) {
System.out.println("key: " + entry.getKey() + " value: " + entry.getValue());
}
}
}


Output
Iterating or looping map using entrySet and foreach loop
key: Key2 value: Value2
key: Key1 value: Value1

4. Iterate or Loop HashMap in Java Using entrySet() and java iterator

In this example we have used combination of Iterator and entrySet() to show all keys and values of a Java Map.


import java.util.*;

public class HashMapLoopExample {
public static void main(String args[]) {

// Creating a HashMap of String keys and String values
HashMapString, String> hashmap = new HashMapString, String>();
hashmap.put("Key1", "Value1");
hashmap.put("Key2", "Value2");
System.out.println("Iterating or looping map using entrySet and Iterator");
// Iterating or looping using entrySet() method
SetMap.EntryString, String>> entrySet1 = hashmap.entrySet();
IteratorMap.EntryString, String>> entrySetIterator = entrySet1.iterator();
while (entrySetIterator.hasNext()) {
Map.Entry entry = entrySetIterator.next();
System.out.println("key: " + entry.getKey() + " value: " + entry.getValue());
}
}
}


Output
Iterating or looping map using entrySet and Iterator
key: Key2 value: Value2
key: Key1 value: Value1

I have shown all the ways through which looping of Map in java is possible. Please mention in comments if you know any other way of iterating or looping of Map in java.


This post first appeared on Java Hungry, please read the originial post: here

Share the post

How to iterate or loop over HashMap (Map) in Java with Example

×

Subscribe to Java Hungry

Get updates delivered right to your inbox!

Thank you for your subscription

×