Java - HashMap

Java HashMap is a data structure that stores key-value pairs in a map. It allows us to store, retrieve and manipulate data in a very efficient way. The keys in the HashMap should be unique and the values can be duplicate. It is a part of the Java Collection framework and is similar to a Hashtable, but it is not synchronized.

To use a HashMap, you need to import the java.util.HashMap package.

Here is an example of how to create a HashMap and add key-value pairs to it:

import java.util.HashMap;
public class Example {
    public static void main(String[] args) {
        // Create a new HashMap
        HashMap<String, Integer> scores = new HashMap<>();

        // Add some key-value pairs to the HashMap
        scores.put("Alice", 90);
        scores.put("Bob", 80);
        scores.put("Charlie", 95);

        // Access a value by its key
        int aliceScore = scores.get("Alice");
        System.out.println("Alice's score is " + aliceScore);

        // Modify a value
        scores.put("Bob", 85);

        // Remove a key-value pair
        scores.remove("Charlie");

        // Loop over the keys in the HashMap
        for (String key : scores.keySet()) {
            int value = scores.get(key);
            System.out.println(key + " has a score of " + value);
        }
    }
}

In this example, we first create a new HashMap called scores that maps String keys to Integer values. We then add three key-value pairs to the HashMap using the put() method.

To access a value in the HashMap, we use the get() method and pass in the key. In this case, we retrieve Alice's score and store it in a variable called aliceScore.

We can modify a value in the HashMap by calling the put() method again with the same key but a new value. In this case, we update Bob's score to 85.

We can remove a key-value pair from the HashMap by calling the remove() method and passing in the key.

Finally, we loop over the keys in the HashMap using a for-each loop and print out each key-value pair using the keySet() method to get a set of all the keys and then retrieve each value using the get() method.

Iterating over a HashMap:

HashMap<String, Integer> map = new HashMap<>();
map.put("apple", 1);
map.put("banana", 2);
map.put("orange", 3);

for (String key : map.keySet()) {
    System.out.println(key + " = " + map.get(key));
}

Removing an element from a HashMap:

HashMap<String, Integer> map = new HashMap<>();
map.put("apple", 1);
map.put("banana", 2);
map.put("orange", 3);

map.remove("banana");

for (String key : map.keySet()) {
    System.out.println(key + " = " + map.get(key));
}

Checking if a key exists in a HashMap:

HashMap<String, Integer> map = new HashMap<>();
map.put("apple", 1);
map.put("banana", 2);
map.put("orange", 3);

if (map.containsKey("banana")) {
    System.out.println("Banana exists in the map.");
}

Getting the size of a HashMap:

HashMap<String, Integer> map = new HashMap<>();
map.put("apple", 1);
map.put("banana", 2);
map.put("orange", 3);

int size = map.size();
System.out.println("The size of the map is: " + size);