import java.util.*;
class MapInterfaceExample
{
public static void main(String args[])
{
// Creating a HashMap
Map<Integer, String> map = new HashMap<Integer, String>();
// Adding elements to the map
map.put(100, "Rajendra");
map.put(101, "Sarvam");
map.put(102, "TechSarvam");
// Iterating through the map
for (Map.Entry<Integer, String> m : map.entrySet())
{
System.out.println(m.getKey() + " " + m.getValue());
}
}
}
Step-by-Step Explanation:
1. Importing the Necessary Classes:
import java.util.*;
This line imports the classes from the java.util package. In this case, we will require the Map, HashMap, and Map. Entry classes to use the map and iterate over its entries.
2. Class Declaration:
class MapInterfaceExample {
This line declares a class named MapInterfaceExample. The class includes the main() method where the program’s execution starts.
3. Main Method:
public static void main(String args[]) {
This is the entry point of the Java program. The main() method is where the program starts its execution.
4. Creating a HashMap Instance:
Map<Integer, String> map = new HashMap<Integer, String>();
- A reference variable map of the Map interface is created, and an instance of HashMap is initialized.
- It is one type of the Map implementation class to store its element as key and value in terms of pair form.
- Map is parameterized with <>Integer, String>, this means it used keys with a data type as Integer, values as data types of a String.
5. Adding Key-Value Pairs to the Map:
map.put(100, “Rajendra”);
map.put(101, “Sarvam”);
map.put(102, “TechSarvam”);
The put() method is used to add key-value pairs to the map:
- Key 100 is associated with the value “Rajendra”.
- Key 101 is associated with the value “Sarvam”.
- Key 102 is associated with the value “TechSarvam”.
6. Iterating Through the Map:
for (Map.Entry m : map.entrySet()) {
System.out.println(m.getKey() + ” ” + m.getValue());
}
- map.entrySet(): Returns a Set of key-value pairs (Map.Entry) in the map. Each Map.Entry represents a single key-value pair.
for (Map.Entry m : map.entrySet()): This loop iterates over each entry in the map:
- For each Map.Entry, m.getKey() retrieves the key and m.getValue() retrieves the value.
- System.out.println(m.getKey() + ” ” + m.getValue());: This line prints each key and its associated value.
7. Output:
- The program will print all the key-value pairs in the map.
- The output order is not guaranteed to be in the order of insertion because HashMap does not maintain any specific order of the elements. The elements will be printed in some random order based on the internal hashing mechanism.
Sample Output (order may vary):
100 Rajendra
101 Sarvam
102 TechSarvam
Key Concepts Explanation
- Map Interface: The Map interface in Java is a collection of key-value pairs. Each key in the map is unique, and each key maps to exactly one value.
- HashMap: HashMap is a concrete implementation of the Map interface. It stores the map entries in a hash table and does not guarantee any specific order of the elements.
- Map.Entry: The interface Map.Entry is a key-value pair in the Map. The key can be obtained by getKey() method and the value can be obtained by getValue() method.
- Iteration: In this step, we will make use of for-each loop that iterates through the set of Map. Entry elements, and for every entry, key and value will be printed.
Time Complexity:
- Insertion (put()): O(1) average time complexity to insert elements into a HashMap because it uses hashing, but in the worst case if there are many collisions, it could take O(n).
- Iteration (entrySet() and for-each loop): O(n), where n is the number of elements in the map.