Java Program to Iterate over a HashMap



In this article, we will understand how to iterate over a HashMap. Java HashMap is a hash table based implementation of Java's Map interface. It is a collection of key-value pairs.

Below is a demonstration of the same −

Suppose our input is

Input Hashmap: {Java=Enterprise, JavaScript=Frontend, Mysql=Backend, Python=ML/AI}

The desired output would be

The keys of the Hashmap are: Java, JavaScript, Mysql, Python,
The Values of the Hashmap are: Enterprise, Frontend, Backend, ML/AI,

Algorithm

Step 1 - START
Step 2 - Declare namely
Step 3 - Define the values.
Step 4 - Create a hashmap of strings and initialize elements in it using the ‘put’ method.
Step 5 - Display the hashmap on the console.
Step 6 - Iterate over the elements of the hashmap, and fetch each key using ‘keySet’ method.
Step 7 - Display this on the console.
Step 6 - Stop

Example 1

Here, we bind all the operations together under the ‘main’ function.

Open Compiler
import java.util.HashMap; import java.util.Map.Entry; public class Demo { public static void main(String[] args) { System.out.println("The required packages have been imported"); HashMap<String, String> input_map = new HashMap<>(); input_map.put("Java", "Enterprise"); input_map.put("Python", "ML/AI"); input_map.put("JavaScript", "Frontend"); input_map.put("Mysql", "Backend"); System.out.println("The HashMap is defined as: " + input_map); System.out.print("\nThe keys of the Hashmap are: "); for(String key: input_map.keySet()) { System.out.print(key); System.out.print(", "); } System.out.print("\nThe Values of the Hashmap are: "); for(String value: input_map.values()) { System.out.print(value); System.out.print(", "); } } }

Output

The required packages have been imported
The HashMap is defined as: {Java=Enterprise, JavaScript=Frontend, Mysql=Backend, Python=ML/AI}

The keys of the Hashmap are: Java, JavaScript, Mysql, Python,
The Values of the Hashmap are: Enterprise, Frontend, Backend, ML/AI,

Example 2

Here, we encapsulate the operations into functions exhibiting object oriented programming.

Open Compiler
import java.util.HashMap; public class Demo { static void print_keys(HashMap<String, String> input_map){ System.out.print("\nThe keys of the Hashmap are: "); for(String key: input_map.keySet()) { System.out.print(key); System.out.print(", "); } } static void print_values( HashMap<String, String> input_map){ System.out.print("\nThe Values of the Hashmap are: "); for(String value: input_map.values()) { System.out.print(value); System.out.print(", "); } } public static void main(String[] args) { System.out.println("The required packages have been imported"); HashMap<String, String> input_map = new HashMap<>(); input_map.put("Java", "Enterprise"); input_map.put("Python", "ML/AI"); input_map.put("JavaScript", "Frontend"); input_map.put("Mysql", "Backend"); System.out.println("The HashMap is defined as: " + input_map); print_keys(input_map); print_values(input_map); } }

Output

The required packages have been imported
The HashMap is defined as: {Java=Enterprise, JavaScript=Frontend, Mysql=Backend, Python=ML/AI}

The keys of the Hashmap are: Java, JavaScript, Mysql, Python,
The Values of the Hashmap are: Enterprise, Frontend, Backend, ML/AI,
Updated on: 2022-03-30T09:08:14+05:30

443 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements