Iterate through a HashMap

0 votes
What's the best way to iterate over the items in a HashMap?
Dec 22, 2020 in Java by Roshni
• 10,520 points
714 views

1 answer to this question.

0 votes

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
    }
}
answered Dec 22, 2020 by Gitika
• 65,890 points

Related Questions In Java

0 votes
0 answers

Ho do I Iterate through a HashMap which contains duplicate values

What is the fastest and the best ...READ MORE

Apr 16, 2018 in Java by Daisy
• 8,140 points
797 views
0 votes
2 answers

Ho do I Iterate through a HashMap which contains duplicate values

for (Map.Entry<String, String> item : params.entrySet()) { ...READ MORE

answered Jul 24, 2018 in Java by samarth295
• 2,220 points
5,254 views
0 votes
2 answers

How to iterate in a HashMap in Java?

Iterating using Iterator. Using Generics: Map<Integer, Integer> map = ...READ MORE

answered Aug 28, 2019 in Java by Sirajul
• 59,230 points
1,006 views
0 votes
1 answer
0 votes
2 answers

How is hashmap different from hashtable?

HashMap and HashTable: 1) Hashtable and Hashmap implement ...READ MORE

answered Aug 1, 2018 in Java by samarth295
• 2,220 points
6,395 views
0 votes
2 answers

What is the easiest way to iterate through the characters of a string in Java?

There are two approaches to this: for(int i ...READ MORE

answered Aug 19, 2019 in Java by Sirajul
• 59,230 points
1,824 views
0 votes
1 answer

How do I efficiently iterate over each entry in a Java Map?

To answer your questions, use the following ...READ MORE

answered Feb 8, 2022 in Java by Soham
• 9,710 points
620 views
+5 votes
4 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,450 points

edited Nov 7, 2018 by Omkar 81,112 views
+1 vote
12 answers

Iterate over a JSONObject?

Assuming your JSON object is saved in ...READ MORE

answered Dec 11, 2020 in Java by Rajiv
• 8,910 points
124,615 views
0 votes
1 answer

What are the differences between a HashMap and a Hashtable in Java?

There are several differences between HashMap and Hashtable in Java: Hashtable is synchronized, whereas HashMap is ...READ MORE

answered Dec 21, 2020 in Java by Gitika
• 65,890 points
1,029 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP