bg_image
header

Hash Map

A Hash Map (also known as a hash table) is a data structure used to store key-value pairs efficiently, providing average constant time complexity (O(1)) for search, insert, and delete operations. Here are the fundamental concepts and workings of a hash map:

Fundamental Principles of a Hash Map

  1. Key-Value Pairs: A hash map stores data in the form of key-value pairs. Each key is unique and is used to access the associated value.
  2. Hash Function: A hash function takes a key and converts it into an index that points to a specific storage location (bucket) in the hash map. Ideally, this function should evenly distribute keys across buckets to minimize collisions.
  3. Buckets: A bucket is a storage location in the hash map that can contain multiple key-value pairs, particularly when collisions occur.

Collisions and Their Handling

Collisions occur when two different keys generate the same hash value and thus the same bucket. There are several methods to handle collisions:

  1. Chaining: Each bucket contains a list (or another data structure) where all key-value pairs with the same hash value are stored. In case of a collision, the new pair is simply added to the list of the corresponding bucket.
  2. Open Addressing: All key-value pairs are stored directly in the array of the hash map. When a collision occurs, another free bucket is searched for using probing techniques such as linear probing, quadratic probing, or double hashing.

Advantages of a Hash Map

  • Fast Access Times: Thanks to the hash function, search, insert, and delete operations are possible in average constant time.
  • Flexibility: Hash maps can store a variety of data types as keys and values.

Disadvantages of a Hash Map

  • Memory Consumption: Hash maps can require more memory, especially when many collisions occur and long lists in buckets are created or when using open addressing with many empty buckets.
  • Collisions: Collisions can degrade performance, particularly if the hash function is not well-designed or the hash map is not appropriately sized.
  • Unordered: Hash maps do not maintain any order of keys. If an ordered data structure is needed, such as for iteration in a specific sequence, a hash map is not the best choice.

Implementation Example (in Python)

Here is a simple example of a hash map implementation in Python:

class HashMap:
    def __init__(self, size=10):
        self.size = size
        self.map = [[] for _ in range(size)]
        
    def _get_hash(self, key):
        return hash(key) % self.size
    
    def add(self, key, value):
        key_hash = self._get_hash(key)
        key_value = [key, value]
        
        for pair in self.map[key_hash]:
            if pair[0] == key:
                pair[1] = value
                return True
        
        self.map[key_hash].append(key_value)
        return True
    
    def get(self, key):
        key_hash = self._get_hash(key)
        for pair in self.map[key_hash]:
            if pair[0] == key:
                return pair[1]
        return None
    
    def delete(self, key):
        key_hash = self._get_hash(key)
        for pair in self.map[key_hash]:
            if pair[0] == key:
                self.map[key_hash].remove(pair)
                return True
        return False
    
# Example usage
h = HashMap()
h.add("key1", "value1")
h.add("key2", "value2")
print(h.get("key1"))  # Output: value1
h.delete("key1")
print(h.get("key1"))  # Output: None

In summary, a hash map is an extremely efficient and versatile data structure, especially suitable for scenarios requiring fast data access times.

 


Random Tech

Open Web Application Security Project - OWASP


owasp.png