Write a Java Program to Iterate over a HashMap

Java Program to Iterate over a HashMap

Welcome to the World of Online Learning:

Hello Friends “This blog helps you to learn Java programming concepts. You can learn Java  language at your own speed and time. One can learn concepts of Java language by practicing various programs given on various pages of this blog. Enjoy the power of Self-learning using the Internet.”

Java Program to Iterate over a HashMap
Java Program to Iterate over a HashMap

Write a Java Program to Iterate over a HashMap

PROGRAM: Java Program to Iterate over a HashMap

/*Java Program to Iterate over a HashMap*/

1: Iterate through HashMap using the forEach loop

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

class Main {
public static void main(String[] args) {

// Creating a HashMap
HashMap<String, String> languages = new HashMap<>();
languages.put(“Java”, “Enterprise”);
languages.put(“Python”, “ML/AI”);
languages.put(“JavaScript”, “Frontend”);
System.out.println(“HashMap: ” + languages);

// iterating through key/value mappings
System.out.print(“Entries: “);
for(Entry<String, String> entry: languages.entrySet()) {
System.out.print(entry);
System.out.print(“, “);
}

// iterating through keys
System.out.print(“\nKeys: “);
for(String key: languages.keySet()) {
System.out.print(key);
System.out.print(“, “);
}

// iterating through values
System.out.print(“\nValues: “);
for(String value: languages.values()) {
System.out.print(value);
System.out.print(“, “);
}
}
}

2: Iterate through HashMap using iterator()

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

class Main {
public static void main(String[] args) {
// create a HashMap
HashMap<String, String> languages = new HashMap<>();
languages.put(“Java”, “Enterprise”);
languages.put(“Python”, “ML/AI”);
languages.put(“JavaScript”, “Frontend”);
System.out.println(“HashMap: ” + languages);

// create an object of Iterator
Iterator<Entry<String, String>> iterate1 = languages.entrySet().iterator();

// iterate through key/value mappings
System.out.print(“Entries: “);
while(iterate1.hasNext()) {
System.out.print(iterate1.next());
System.out.print(“, “);
}

// iterate through keys
Iterator<String> iterate2 = languages.keySet().iterator();
System.out.print(“\nKeys: “);
while(iterate2.hasNext()) {
System.out.print(iterate2.next());
System.out.print(“, “);
}

// iterate through values
Iterator<String> iterate3 = languages.values().iterator();
System.out.print(“\nValues: “);
while(iterate3.hasNext()) {
System.out.print(iterate3.next());
System.out.print(“, “);
}
}
}



Leave a Reply

Your email address will not be published. Required fields are marked *