---
title: "What is the best way to iterate on hashmap in java"  
description: "What is the best way to iterate on hashmap in java"  
author: "Anonymous User"  
published: 2015-07-27  
updated: 2015-07-27  
canonical: https://www.mindstick.com/forum/23377/what-is-the-best-way-to-iterate-on-hashmap-in-java  
category: "java"  
tags: ["java", "collection"]  
reading_time: 1 minute  

---

# What is the best way to iterate on hashmap in java

What's the best way to iterate over the [items](https://www.mindstick.com/forum/33812/getting-number-of-items-selected-in-uicollectionview-in-ios) in a [HashMap](https://www.mindstick.com/forum/159211/what-are-the-differences-between-a-hashmap-and-a-hashtable-in-java)?

## Replies

### Reply by Anonymous User

Iterate through the ***entrySet*** like so:

```
public static void printMap(Map mp) {    Iterator it = mp.entrySet().iterator();    while (it.hasNext()) {        Map.Entry pair = (Map.Entry)it.next();        System.out.println(pair.getKey() + " = " + pair.getValue());        it.remove(); // avoids a ConcurrentModificationException    }}
```


---

Original Source: https://www.mindstick.com/forum/23377/what-is-the-best-way-to-iterate-on-hashmap-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
