What is the difference between HashMap and ConcurrentHashMap?
HashMap is not thread-safe. ConcurrentHashMap is designed for safe concurrent reads and updates without locking the entire map.
Interview Prep
Core Java, collections, concurrency, JVM, streams, records, and practical senior-level Java interview prep.
Showing 20 of 20 questions
HashMap is not thread-safe. ConcurrentHashMap is designed for safe concurrent reads and updates without locking the entire map.
volatile gives visibility guarantees for a variable. synchronized gives visibility plus mutual exclusion, meaning only one thread can enter the critical section at a time.
Comparable defines an object's natural ordering (comparison with equals method) inside the class. Comparator defines external sorting strategies, which lets the same objects be sorted in multiple ways.
synchronized is Java's built-in locking mechanism. ReentrantLock gives similar mutual exclusion but adds more control, such as tryLock, timed waits, interruptible locking, fairness, and explicit unlock behavior.
AtomicInteger updates one shared value atomically. LongAdder spreads updates across multiple internal cells to reduce contention, making it better for high-throughput counters.
A thread pool is a group of reusable worker threads that process tasks from a queue, helping avoid the cost and chaos of creating a new thread for every request.
The JVM organizes runtime memory into areas like the heap, stack, method area, PC register, and native method stack. The key interview idea is knowing what is shared across threads, what is private to each thread, and where objects, method calls, and class metadata live.
wait/notify is tied to synchronized and intrinsic object monitors. Condition is tied to explicit locks like ReentrantLock and allows multiple named wait queues for clearer thread coordination.
Runnable represents work that runs without returning a result. Callable represents work that can return a result and throw checked exceptions, usually accessed through a Future.
HashMap stores key-value pairs in buckets. It uses the key's hash code to find a bucket, handles collisions inside that bucket, and resizes when the map becomes too full.
ThreadLocal lets each thread store its own isolated value, even when the same ThreadLocal variable is shared. It is useful for request context, user context, tracing IDs, and avoiding parameter-passing clutter, but it must be cleaned up carefully in thread pools.
ExecutorService separates task submission from thread management. A thread pool reuses a limited number of worker threads instead of creating a new thread for every task.
Understand List.of(), Set.of(), Map.of(), unmodifiable wrappers, shallow immutability, and common interview follow-ups.
HashSet is usually used when you only need fast uniqueness. TreeSet is used when you need uniqueness plus sorted order.
Future represents a result that may be available later. CompletableFuture adds async composition: chaining, combining, transforming, and handling errors without immediately blocking.
Learn the most common mistakes developers make with Optional and how to use it effectively in modern Java.
Streams are powerful for filtering, mapping, grouping, and aggregating data, but they can also make code harder to read when misused.
Structured concurrency treats related concurrent tasks as one unit of work, making cancellation, failure handling, and task ownership easier to reason about.
Virtual threads are lightweight Java threads finalized in Java 21. They make thread-per-task style programming practical for many high-concurrency blocking I/O workloads.
A thread dump is a snapshot of every thread in a JVM. Learn how to identify deadlocks, blocked threads, lock contention, and stuck requests.