In this article we will focus on how to iterate through hashmap. There are many ways to iterate through HashMap’s in java. We will try some of them.
Methods to iterate through hashmap
Using for-each loop we can loop through the keySet or the values or the entrySet by the following methods
HashMap.keySet()
HashMap.values()
HashMap.entrySet()
Iterators are also used to loop through the hashmaps.
For example we will take a Hashmap and add data to it
1 2 3 4 5 |
HashMap<String, String> userMap = new HashMap<String, String>(); userMap.put("1", "John Doe"); userMap.put("2", "Stuart Little"); userMap.put("3", "Scott Tiger"); |
Display keys
1 2 3 |
for (String key : userMap.keySet()) { System.out.println("Key :: " + key); } |
Display values
1 2 3 |
for (String values : userMap.values()) { System.out.println("Values :: " + values); } |
Display Keys and values using keySet
and get
method of hashMap
1 2 3 |
for (String key : userMap.keySet()) { System.out.println(key + " :: " + userMap.get(key)); } |
Display key and values using entrySet()
method
1 2 3 |
for (Map.Entry<String, String> entry : userMap.entrySet()) { System.out.println(entry.getKey() + " :: " + entry.getValue()); } |
Display key and values using Iterator
1 2 3 4 5 6 |
Iterator<Map.Entry<String, String>> iterator = userMap.entrySet() .iterator(); while (iterator.hasNext()) { Map.Entry<String, String> entry = iterator.next(); System.out.println(entry.getKey() + " :: " + entry.getValue()); } |
Below is the complete java program that has all the methods to loop through a Hashmap and display keys/values or a key value pair.
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 43 44 45 46 47 48 49 50 51 52 |
package com.kscodes.sampleproject; import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class HashMapTest { /** * @param args */ public static void main(String[] args) { HashMap<String, String> userMap = new HashMap<String, String>(); userMap.put("1", "John Doe"); userMap.put("2", "Stuart Little"); userMap.put("3", "Scott Tiger"); System.out.println("For Each Method"); System.out.println("Printing only Keys"); for (String key : userMap.keySet()) { System.out.println("Key :: " + key); } System.out.println("Printing only Values"); for (String values : userMap.values()) { System.out.println("Values :: " + values); } System.out.println("Printing both Key and Values using only keys"); for (String key : userMap.keySet()) { System.out.println(key + " :: " + userMap.get(key)); } System.out.println("Printing both Key and Values using EntrySet"); for (Map.Entry<String, String> entry : userMap.entrySet()) { System.out.println(entry.getKey() + " :: " + entry.getValue()); } System.out.println("Printing both Key and Values using Iterator"); Iterator<Map.Entry<String, String>> iterator = userMap.entrySet() .iterator(); while (iterator.hasNext()) { Map.Entry<String, String> entry = iterator.next(); System.out.println(entry.getKey() + " :: " + entry.getValue()); } } } |