What is HashMap in Java? Explain with example

HashMap in Java

A HashMap in Java is part of the Java Collections Framework and provides the basic implementation of the Map interface. A HashMap stores data in key-value pairs, allowing efficient retrieval of values based on their corresponding keys. Here are some key characteristics of a HashMap:

  • Hashing: Internally, a HashMap uses an array of buckets. Each bucket can hold multiple key-value pairs, which are stored in linked lists. The position of each key-value pair in the array is determined by the hash code of the key.
  • Unique Keys: Each key in a HashMap must be unique. If you try to insert a duplicate key, the old value associated with that key will be replaced.
  • Null Values: A HashMap allows null values and the null key.

Here’s an example demonstrating how to use a HashMap in Java:

import java.util.HashMap;

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

        // Adding key-value pairs to the HashMap
        map.put("Alice", 30);
        map.put("Bob", 25);
        map.put("Charlie", 35);

        // Retrieving a value based on key
        int ageOfAlice = map.get("Alice");
        System.out.println("Alice's age: " + ageOfAlice);

        // Checking if a key exists
        if (map.containsKey("Bob")) {
            System.out.println("Bob is in the map.");
        }

        // Checking if a value exists
        if (map.containsValue(35)) {
            System.out.println("There is someone who is 35 years old.");
        }

        // Iterating over key-value pairs
        for (String key : map.keySet()) {
            System.out.println("Key: " + key + ", Value: " + map.get(key));
        }

        // Removing a key-value pair
        map.remove("Charlie");

        // Checking the size of the HashMap
        System.out.println("Size of the map: " + map.size());
    }
}

Explanation of HashMap with Example

  • Creating a HashMap: HashMap<String, Integer> map = new HashMap<>();
    • Creates a HashMap where keys are of type String and values are of type Integer.
  • Adding Key-Value Pairs: map.put("Alice", 30);
    • Adds a key-value pair to the HashMap. The key is "Alice" and the value is 30.
  • Retrieving a Value: map.get("Alice");
    • Retrieves the value associated with the key "Alice", which is 30.
  • Checking for a Key: map.containsKey("Bob");
    • Checks if the key "Bob" exists in the HashMap.
  • Checking for a Value: map.containsValue(35);
    • Checks if the value 35 exists in the HashMap.
  • Iterating over Key-Value Pairs:
    • Uses a for-each loop to iterate over the keys of the HashMap and print each key-value pair.
  • Removing a Key-Value Pair: map.remove("Charlie");
    • Removes the key-value pair with the key "Charlie" from the HashMap.
  • Checking the Size of the HashMap: map.size();
    • Returns the number of key-value pairs in the HashMap.

This example demonstrates basic operations such as adding, retrieving, checking, iterating, and removing key-value pairs in a HashMap.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *