Java Program to Iterate Over a HashMap
What is HashMap in Java ?
A hashmap is a data structure used for storing and organizing key-value pairs. It allows for quick lookup of values associated with a given key, making it a very efficient way to store and retrieve data.
The basic idea behind a hashmap is to use a hash function to compute a unique index for each key-value pair. This index is used to store and retrieve the value associated with that key. The hash function takes the key as input and returns an integer, which is used to determine the index in the array where the value will be stored.
How HashMap Works ?
When a value is added to a hashmap, its key is passed through the hash function to determine the index at which the value should be stored. If that index is already occupied by another key-value pair, a collision occurs. There are various ways to handle collisions, such as using linked lists or other data structures to store multiple values at the same index.
Advantages of Using HashMap :
One of the main advantages of a hashmap is that it allows for very fast lookup of values based on their keys. In a properly designed hashmap, the time required to find a value is independent of the size of the hashmap, making it a scalable data structure for many applications.
Ways to Iterate over HashMap :
Syntax :
Map<String, Integer> map = new HashMap<>(); map.put("A", 1); map.put("B", 2); map.put("C", 3); for (Map.Entry<String, Integer> entry : map.entrySet()) { String key = entry.getKey(); int value = entry.getValue(); System.out.println("Key: " + key + ", Value: " + value); }
Syntax :
Map<String, Integer> map = new HashMap<>(); map.put("A", 1); map.put("B", 2); map.put("C", 3); for (String key : map.keySet()) { int value = map.get(key); System.out.println("Key: " + key + ", Value: " + value); }
Syntax :
Mapmap = new HashMap<>(); map.put("A", 1); map.put("B", 2); map.put("C", 3); Iterator > iterator = map.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry entry = iterator.next(); String key = entry.getKey(); int value = entry.getValue(); System.out.println("Key: " + key + ", Value: " + value); }
Example 1 :
import java.util.HashMap; import java.util.Map; public class Main { public static void main(String[] args) { Map< String, Integer> map = new HashMap<>(); map.put("A", 1); map.put("B", 2); map.put("C", 3); for (Map.Entry< String, Integer> entry : map.entrySet()) { String key = entry.getKey(); int value = entry.getValue(); System.out.println("Key: " + key + ", Value: " + value); } } }
Output :
Key: A, Value: 1 Key: B, Value: 2 Key: C, Value: 3
Example 2 :
import java.util.HashMap; import java.util.Map; public class Main { public static void main(String[] args) { Map< String, Integer> map = new HashMap<>(); map.put("A", 1); map.put("B", 2); map.put("C", 3); for (String key : map.keySet()) { int value = map.get(key); System.out.println("Key: " + key + ", Value: " + value); } } }
Output :
Key: A, Value: 1 Key: B, Value: 2 Key: C, Value: 3
Example 2 :
import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class Main { public static void main(String[] args) { Map< String, Integer> map = new HashMap<>(); map.put("A", 1); map.put("B", 2); map.put("C", 3); Iterator< Map.Entry< String, Integer>> iterator = map.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry< String, Integer> entry = iterator.next(); String key = entry.getKey(); int value = entry.getValue(); System.out.println("Key: " + key + ", Value: " + value); } } }
Output :
Key: A, Value: 1 Key: B, Value: 2 Key: C, Value: 3
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment