ReviseAlgo Logo

Hash Maps & Sets

HashMap Operations

Master the essential HashMap APIs across Java, Python, and C++ including insertion, lookup, removal, presence checking, and iteration.

Last Updated: August 2, 2026 15 min read

1. Introduction

What are HashMap Operations?

HashMap Operations are the primary actions used to interact with a hash table: inserting keys (writing), looking up values (reading), checking if a key exists, removing entries, and iterating through the entire collection.

Why study them?

Every programming language has its own unique HashMap naming conventions, syntax, and behaviors. Knowing how to write clean, crash-free map operations is key to passing coding interviews.

Where is it Used?

  • Tracking Frequency: Finding the count of characters in a string.
  • Caching: Storing computed results during dynamic programming recursion.

  • 2. Mental Model: The Rolodex

    Think of a HashMap as a Rolodex (a rolling contact card file):

  • Insert: Write a name and a phone number on a card and slide it in.
  • Lookup: Flip to the letter tab and read the phone number.
  • Remove: Pull the card out and shred it.
  • Iterate: Flip through every card in the deck one by one.

  • 3. Core API Implementations

    Here is a side-by-side comparison of standard HashMap and HashSet operations across major languages:


    4. Visual Cross-Language Translation Table

    ActionJavaPythonC++Complexity
    Create Mapnew HashMap<>(){}unordered_mapO(1)
    Insert/Updatemap.put(k, v)map[k] = vmap[k] = vO(1) avg
    Lookupmap.get(k)map[k]map[k]O(1) avg
    Presence Checkmap.containsKey(k)k in mapmap.count(k) > 0O(1) avg
    Deletemap.remove(k)del map[k]map.erase(k)O(1) avg
    Set Insertset.add(v)set.add(v)set.insert(v)O(1) avg
    Set Checkset.contains(v)v in setset.count(v) > 0O(1) avg

    5. Real-World Applications

  • Caching Server Queries: Storing JSON outputs by search query keyword.
  • User Authentication Registry: Mapping usernames to hashed passwords for user accounts validation.

  • 6. Interview Perspective

    How Interviewers Ask This Topic

    Interviewers verify whether you know language-specific behaviors:
  • C++ operator [] Side-effects: In C++, executing map[key] when key does not exist automatically creates an entry with a default value (like 0 for integers). Use map.find() or map.count() for checking presence.
  • Python KeyError Exceptions: Looking up d[key] directly in Python triggers a KeyError if the key is missing. Use d.get(key, default) or check key in d first.
  • Common Mistakes

    Warning: 1. Accessing Missing Keys without Checks: Writing map.get(key).intValue() in Java when the key is missing throws a NullPointerException. Always use getOrDefault or containsKey.
    > 2. Modifying Collections during Iteration: Removing items from a Map directly while looping through its entrySet triggers a ConcurrentModificationException in Java. Use an Iterator or collect keys to delete in a separate list first.

    7. Summary

  • Constant Time: Put, get, delete, and contains are O(1) average time.
  • Safe Lookups: Use getOrDefault (Java/Python) or iterator checks (C++) to avoid crashes.
  • Iteration: Yields keys/values in undefined order.

  • 8. Quiz

    Question 1: What is the risk of using map[key] to check for key existence in C++? Answer: If the key is not in the map, map[key] inserts the key with a default value (e.g. 0 or empty string). This modifies the map unnecessarily and increases memory footprint. Use map.count(key) or map.find(key) instead.
    Question 2: How do you safely look up a key in Python dictionaries to avoid KeyError crashes? Answer: Use dictionary.get(key, default_value), which returns default_value if the key does not exist.
    Question 3: In Java, what is the difference between map.put(key, val) and map.putIfAbsent(key, val)? Answer: map.put overwrites the existing value if the key is present. map.putIfAbsent only inserts the key-value pair if the key is not already present (or is mapped to null).
    Question 4: Can a HashSet contain duplicate values? Answer: No. Sets are mathematically collections of unique elements. Attempting to add an existing value has no effect and returns false (in Java).
    Question 5: What is the time complexity of checking if a value (not key) exists in a HashMap? Answer: O(N) time. Because hashmaps only index by key, finding a value requires scanning through all values in the bucket array.