Understanding Java HashMap for DSA

A HashMap in Java is a part of the Java Collections Framework and provides a way to store key-value pairs, where keys are unique. It is part of the java.util package and implements the Map interface. The HashMap class allows null values and the null key, and it does not guarantee the order of its elements over time.

Key Characteristics

  1. Key-Value Pairs: Stores data in key-value pairs. Each key is associated with exactly one value.

  2. Unique Keys: Keys are unique; duplicate keys are not allowed.

  3. Null Values: Allows one null key and multiple null values.

  4. Unordered: This does not guarantee any specific order of the elements.

  5. Not Synchronized: The HashMap itself is not synchronized. For concurrent access, use ConcurrentHashMap.

Constructor

  1. Default Constructor: This creates an empty HashMap with the default initial capacity (16) and the default load factor (0.75).

     HashMap<String, Integer> map = new HashMap<>();
    
  2. Constructor with Initial Capacity: This creates an empty HashMap with the specified initial capacity and the default load factor (0.75).

     HashMap<String, Integer> map = new HashMap<>(20);
    
  3. Constructor with Initial Capacity and Load Factor: This creates an empty HashMap with the specified initial capacity and load factor.

     HashMap<String, Integer> map = new HashMap<>(20, 0.8f);
    
  4. Constructor with Map: This creates a new HashMap with the same mappings as the specified Map

     Map<String, Integer> anotherMap = new HashMap<>();
     anotherMap.put("key1", 1);
     anotherMap.put("key2", 2);
    
     HashMap<String, Integer> map = new HashMap<>(anotherMap);
    

Common Methods

  1. put(K key, V value): Associates the specified value with the specified key in this map.

     hashMap.put("key1", "value1");
    
  2. get(Object key): Returns the value to which the specified key is mapped or null if this map contains no mapping for the key.

     String value = hashMap.get("key1");
    
  3. remove(Object key): Removes the mapping for the specified key from this map if present.

     hashMap.remove("key1");
    
  4. containsKey(Object key): Returns true if this map contains a mapping for the specified key.

     boolean contains = hashMap.containsKey("key1");
    
  5. containsValue(Object value): Returns true if this map maps one or more keys to the specified value.

     boolean contains = hashMap.containsValue("value1");
    
  6. size(): Returns the number of key-value mappings in this map.

     int size = hashMap.size();
    
  7. isEmpty(): Returns true if this map contains no key-value mappings.

     boolean empty = hashMap.isEmpty();
    

Iteration

HashMap can be iterated in various ways such as for-each loop on keys, for-each loop on entries, and forEach method.

for (String key : hashMap.keySet()) {
    System.out.println("Key: " + key + ", Value: " + map.get(key));
}

for (Map.Entry<String, Integer> entry : hashMap.entrySet()) {
    System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue());
}

hashMap.forEach((key, value) -> System.out.println("Key: " + key + ", Value: " + value));

Performance Considerations

  1. Time Complexity: The average time complexity for get, put, and remove operations is O(1). However, in the worst case (due to hash collisions), it can be O(n).

  2. Load Factor: The load factor determines when the capacity of the HashMap is increased. A higher load factor means less space overhead but potentially slower access times.

  3. Initial Capacity: The initial capacity of the HashMap determines the number of buckets when the HashMap is created. Choosing an appropriate initial capacity can reduce the need for resizing.

Thank you for reading!

You can support me by buying me a book.

0
Subscribe to my newsletter

Read articles from Vineeth Chivukula directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Vineeth Chivukula
Vineeth Chivukula

There's this guy who's mad about editing and programming. It's his jam, you know?