Java Java Tutorial Java Methods Java Classes Java File Handling Java Reference



Java HashMap

Java HashMap is a class that implements the Map interface and provides a way to store and retrieve key-value pairs. It is a part of the Java Collections Framework and is used to store and retrieve data in a fast and efficient manner. HashMap is a popular data structure in Java and is widely used in various applications.

The HashMap class is based on the hash table data structure. It uses a hash function to map keys to their corresponding values. The hash function takes the key as input and returns an index in the hash table where the value is stored. This makes the retrieval of values very fast as the hash function can quickly locate the index of the value in the hash table.

HashMap allows null values and null keys. It is not synchronized and is not thread-safe. However, it can be made thread-safe by using the Collections.synchronizedMap() method.

Creating a HashMap

To create a HashMap, we need to import the java.util.HashMap package and create an instance of the HashMap class. We can then add key-value pairs to the HashMap using the put() method.


import java.util.HashMap;

public class MyHashMap {
    public static void main(String[] args) {
        // Creating a HashMap
        HashMap<String, Integer> myMap = new HashMap<>();

        // Adding key-value pairs to the HashMap
        myMap.put("John", 25);
        myMap.put("Mary", 30);
        myMap.put("Bob", 35);

        // Retrieving values from the HashMap
        System.out.println(myMap.get("John")); // Output: 25
        System.out.println(myMap.get("Mary")); // Output: 30
        System.out.println(myMap.get("Bob")); // Output: 35
    }
}

In the above example, we have created a HashMap of type <String, Integer>. We have added three key-value pairs to the HashMap using the put() method. We have then retrieved the values from the HashMap using the get() method.

Iterating over a HashMap

We can iterate over a HashMap using the keySet() method, which returns a Set of all the keys in the HashMap. We can then use a for-each loop to iterate over the Set and retrieve the values from the HashMap using the get() method.


import java.util.HashMap;
import java.util.Set;

public class MyHashMap {
    public static void main(String[] args) {
        // Creating a HashMap
        HashMap<String, Integer> myMap = new HashMap<>();

        // Adding key-value pairs to the HashMap
        myMap.put("John", 25);
        myMap.put("Mary", 30);
        myMap.put("Bob", 35);

        // Iterating over the HashMap
        Set<String> keys = myMap.keySet();
        for (String key : keys) {
            System.out.println(key + " : " + myMap.get(key));
        }
    }
}

In the above example, we have created a HashMap of type <String, Integer>. We have added three key-value pairs to the HashMap using the put() method. We have then iterated over the HashMap using the keySet() method and a for-each loop. We have retrieved the values from the HashMap using the get() method.

Conclusion

Java HashMap is a powerful data structure that provides a fast and efficient way to store and retrieve key-value pairs. It is widely used in various applications and is a part of the Java Collections Framework. HashMap is based on the hash table data structure and uses a hash function to map keys to their corresponding values. It allows null values and null keys and is not synchronized or thread-safe. However, it can be made thread-safe by using the Collections.synchronizedMap() method.

References

Activity