Home page

Tuesday, July 11, 2023

HashMap in JAVA

The HashMap class in Java provides an implementation of the Map interface using a hash table data structure. It stores key-value pairs and allows efficient retrieval and insertion operations. Here's an overview of when and how to use HashMap in Java:

Purpose:

  1. Key-Value Mapping: The main purpose of using a HashMap is to store key-value pairs, where each key is unique within the map.
  2. Efficient Retrieval: HashMap provides fast retrieval of values based on their associated keys. It uses hashing to achieve constant-time complexity O(1) for retrieval operations (on average).
  3. Dynamic Size: HashMap is suitable when the size of the map can vary dynamically. It automatically handles resizing and rehashing to maintain performance.

How to use HashMap:

  1. Import the HashMap class: Before using HashMap, import the java.util.HashMap class.
  2. Create a HashMap instance: Instantiate a HashMap object, specifying the types for the key and value.
  3. Add key-value pairs: Use the put method to add key-value pairs to the HashMap.
  4. Retrieve values: Use the get method to retrieve values based on their keys.
  5. Perform other operations: Use various methods available in HashMap, such as containsKey, remove, size, etc., to perform operations on the map.

Example:

import java.util.HashMap;

public class HashMapExample {

    public static void main(String[] args) {

        // Create a HashMap of names and ages

        HashMap<String, Integer> personMap = new HashMap<>();

        // Add key-value pairs to the HashMap

        personMap.put("Alice", 25);

        personMap.put("Bob", 30);

        personMap.put("Charlie", 35);

        // Retrieve values based on keys

        int age = personMap.get("Bob");

        System.out.println(age); // Output: 30

        // Check if a key is present

        boolean containsKey = personMap.containsKey("Alice");

        System.out.println(containsKey); // Output: true

        // Remove a key-value pair

        personMap.remove("Charlie");

        System.out.println(personMap); // Output: {Alice=25, Bob=30}

        // Get the size of the HashMap

        int size = personMap.size();

        System.out.println(size); // Output: 2

    }

}

In the above example, we create a HashMap called personMap to store names as keys and ages as values. We add key-value pairs to the map, retrieve values based on keys, check if a key is present, remove a key-value pair, and retrieve the size of the map.

Remember the following points when using HashMap:

  • HashMap does not maintain any order of key-value pairs. If you need a specific order, consider using LinkedHashMap.
  • Keys in a HashMap must be unique. If a duplicate key is added, the existing value is replaced with the new one.
  • HashMap allows null as both keys and values.
  • The performance of HashMap depends on a good distribution of hash codes for the keys to avoid collisions.
  • Use HashMap when you need to store key-value pairs and require efficient retrieval based on keys. It provides fast access to values and is suitable for dynamic-sized maps.

No comments:

Post a Comment