Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

How To Iterate Map In Java?

Posted on 02/19/2025 By admin

There are multiple ways to iterate over a Map In Java which we will learn with examples in this post.

The Map interface has a method called entrySet() which returns a collection view of the map as Map.Entry. Entry is an inner interface of the Map interface.

Set> entries = mapData.entrySet();
for (Map.Entry keyValue: entries) { String key = keyValue.getKey(); String value = keyValue.getValue(); System.out.println("Key = " + key + " | Value = " + value);
}

We can retrieve all keys at once using the keySet() method in a Set and then iterate it over to get the associated value.

Set allKeys = mapData.keySet();
for (String key: allKeys) { String value = mapData.get(key); System.out.println("Key = " + key + " | Value = " + value);
}

Unlike keySet() method, we can extract all values at once using the values() method. But we can not backtrack their associated keys. So using this method we can get both keys and values together.

Collection allValues = mapData.values();
for (String value: allValues) { System.out.println("Value = " + value);
}

We can use the iterator() method that provides an iterator over a collection. The Iterator interface takes the place of the Enumeration interface in the Java Collections Framework. You can use this instead of the ‘for’ loop. You can call the iterator() method in the output of all three methods above.

Iterator allKeysUsingIterator = mapData.keySet().iterator();
while (allKeysUsingIterator.hasNext()) { String key = allKeysUsingIterator.next(); String value = mapData.get(key); System.out.println("Key = " + key + " | Value = " + value);
}
package collectionsExamples; import java.util.*; public class IterateMap { public static void main(String[] args) { //create a map data HashMap mapData = new HashMap(); mapData.put("id","1"); mapData.put("name","Amod"); mapData.put("address","New York"); mapData.put("skills","Java Selenium"); mapData.put("mob","969768"); // Iterate using entrySet() method Set> entries = mapData.entrySet(); for(Map.Entry keyValue : entries) { String key = keyValue.getKey(); String value = keyValue.getValue(); System.out.println("Key = " + key + " | Value = " + value); } // Iterate using keySet() method Set allKeys = mapData.keySet(); for(String key : allKeys) { String value = mapData.get(key); System.out.println("Key = " + key + " | Value = " + value); } // Iterate using values() method Collection allValues = mapData.values(); for(String value : allValues) { System.out.println("Value = " + value); } // Iterate using iterate() method Iterator allKeysUsingIterator = mapData.keySet().iterator(); while(allKeysUsingIterator.hasNext()) { String key = allKeysUsingIterator.next(); String value = mapData.get(key); System.out.println("Key = " + key + " | Value = " + value); } }
} 

For all ways except the values() method, the output will be as below. For the values() method way we will just get values, not keys.

Key = skills | Value = Java Selenium
Key = mob | Value = 969768
Key = address | Value = New York
Key = name | Value = Amod
Key = id | Value = 1

We can also use lambda function and Stream concepts which I will cover in the next post.

Thanks for reading.

Uncategorized

Post navigation

Previous Post: Git Tutorial 32 – How To Restore Deleted And Committed But Not Pushed File Without GIT Reset?
Next Post: Postman Tutorial Part 14 – Create, Manage and Use Global Variable In Postman

Related Posts

Introduction To Karate – A Unified Framework for API test-automation, mocks, performance-testing, and UI automation Uncategorized
image – Make Selenium Easy Uncategorized
SelectedGroup – Make Selenium Easy Uncategorized
REST Assured Tutorial 28 – What is Plain Old Java Object (POJO) ? Uncategorized
Postman Tutorial Part 24 -Set, Get and Unset Global & Environment Variables in Postman Scripts – Make Selenium Easy Uncategorized
implicitwait – Make Selenium Easy Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark