Top 10 HashMap Interview Questions Every Fresher Should Know

Top 10 HashMap Interview Questions Every Fresher Should Know Work through these 10 questions out loud. Open any question for the full model answer, the trap to avoid, and the follow-up a panel usually asks.

  1. HashMap vs ConcurrentHashMap — when and why?

    HashMap for single-threaded or external synchronization. ConcurrentHashMap for shared concurrent maps — finer-grained locking than Hashtable. Cannot store null keys/values in...

    Full answer

  2. Explain immutability and why String is immutable.

    Immutable objects cannot change after build — thread-safe without synchronization. String immutability allows intern pool, caching hashCode, and security when used as...

    Full answer

  3. What are weak references and WeakHashMap use cases?

    Attach metadata to objects without extending their lifetime — canonical mapping while object lives. When key GCs, entry drops. Do not rely on timing. For API response caching...

    Full answer

  4. Explain garbage collection basics in the JVM.

    New objects allocate in Eden; minor GC copies survivors to survivor spaces, then old gen. When old gen fills, major/full GC runs — longer pauses. Tune -Xmx, choose G1 for...

    Full answer

  5. What is Optional and when should you use it?

    Use Optional as return type when absence is expected — findById returns Optional<User>. Chain with map/flatMap/orElseThrow. Do not use for fields, method parameters, or Jackson...

    Full answer

  6. volatile vs synchronized — when to use each?

    volatile for single-writer flags (shutdown, initialized) where no read-modify-write. synchronized or java.util.concurrent locks when multiple threads update shared state. For...

    Full answer

  7. Explain the difference between == and .equals() in Java.

    For primitives, == compares values. For objects, == asks if two references point to the same instance. equals() compares content when overridden — String overrides it for...

    Full answer

  8. IdentityHashMap vs HashMap — when would you use IdentityHashMap?

    Use when identity matters not value — tracking object visitation in deep graph walk. Business keys (userId, email) belong in HashMap with proper equals/hashCode....

    Full answer

  9. What do Java Records provide and their limitations?

    DTO: record UserResponse(String id, String name) {} — JSON mapping works. Service layer immutable value objects. Not for entities needing setters/lazy proxies yet. Combine with...

    Full answer

  10. What are sequenced collections in JDK 21, and which calls replace fragile index access?

    I reach for getFirst/getLast on lists and deques instead of magic indexes. For a recent-activity map I use LinkedHashMap as a SequencedMap and pollFirstEntry to evict oldest....

    Full answer

HashMap Interview Questions and Answers · Top 50 HashMap Interview Questions and Answers (2026 Edition) · Top 40 HashMap Interview Questions | Must-Prepare before interview. · HashMap Interview Questions Every Fresher Must Know

All interview question lists · Interview guides · Home · Support the platform