How to iterate over a HashMap in Java

2 Answers

0 votes
import java.util.HashMap;
import java.util.Map.Entry;

public class MyClass {
    public static void main(String args[]) {
        HashMap<String, String> hm = new HashMap<>();

        hm.put("Java", "ABC");
        hm.put("C++", "AAB");
        hm.put("Python", "ACB");
        hm.put("C", "AAA");
        hm.put("PHP", "ACD");

        for (Entry<String, String> entry: hm.entrySet()) {
            System.out.println(entry);
        }

    }
}
 
 
 
 
/*
run:
   
Java=ABC
C++=AAB
C=AAA
PHP=ACD
Python=ACB
 
*/

 



answered Jan 21, 2022 by avibootz
0 votes
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Iterator;

public class MyClass {
    public static void main(String args[]) {
        HashMap<String, String> hm = new HashMap<>();

        hm.put("Java", "ABC");
        hm.put("C++", "AAB");
        hm.put("Python", "ACB");
        hm.put("C", "AAA");
        hm.put("PHP", "ACD");

        Iterator<Entry<String, String>> iterate = hm.entrySet().iterator();

        while (iterate.hasNext()) {
            System.out.println(iterate.next());
        }
    }
}
 
 
 
 
/*
run:
   
Java=ABC
C++=AAB
C=AAA
PHP=ACD
Python=ACB
 
*/

 



answered Jan 21, 2022 by avibootz

Related questions

1 answer 129 views
2 answers 245 views
1 answer 159 views
3 answers 103 views
103 views asked Mar 3, 2025 by avibootz
1 answer 78 views
1 answer 226 views
...