Top 10 Classic Java Interview Questions Every Developer Should Master
This article presents ten essential Java interview questions covering HashMap internals, fail‑fast vs. fail‑safe iterators, BlockingQueue, ConcurrentHashMap usage, List implementation performance, Iterator vs. ListIterator differences, CopyOnWriteArrayList, Iterator vs. Enumeration, HashMap synchronization, and the distinction between IdentityHashMap and HashMap.
1. How does Java's HashMap work?
HashMap is a key‑value data structure where each key maps to a value. When put() is called, the key's hashCode() is computed to locate a bucket, and the entry is stored there. Retrieval uses the key's hashCode() and equals() to find the correct entry. Collisions are resolved with a linked list of entries stored in each bucket.
2. What is a fail‑fast iterator?
A fail‑fast iterator throws ConcurrentModificationException if the underlying collection is modified during iteration. A fail‑safe iterator works on a copy of the collection, so it never throws such an exception.
3. What is Java's BlockingQueue? BlockingQueue is part of the java.util.concurrent package. It supports operations that wait for the queue to become non‑empty when retrieving elements and wait for space to become available when storing elements.
4. When should you use ConcurrentHashMap?
ConcurrentHashMap is ideal for scenarios with many concurrent updates because it allows concurrent reads and writes without locking the entire table, offering better performance than Hashtable.
5. Which List implementation provides the fastest insertion? LinkedList offers the fastest insertion and removal in the middle of the list, while ArrayList provides fast random access and dynamic resizing. For frequent middle insertions, LinkedList is preferable.
6. Differences between Iterator and ListIterator
ListIterator has add(), hasPrevious(), previous(), nextIndex(), previousIndex(), and set() methods; Iterator does not.
ListIterator can traverse the list in both directions; Iterator can only move forward.
ListIterator can report the current index; Iterator cannot.
Both can remove elements, but only ListIterator can modify elements via set().
7. What is CopyOnWriteArrayList and how does it differ from ArrayList?
CopyOnWriteArrayList is a thread‑safe variant of ArrayList where all mutating operations create a fresh copy of the underlying array. Writes are slower due to copying, but reads are fast and do not require locking, making it suitable when reads vastly outnumber writes.
8. Differences between Iterator and Enumeration
Iterator allows removal of elements from the underlying collection; Enumeration does not.
Iterator's method names are standardized (hasNext, next, remove); Enumeration uses hasMoreElements() and nextElement().
9. How to synchronize a HashMap?
You can wrap a HashMap with Collections.synchronizedMap(...) or use ConcurrentHashMap. The preferred choice is ConcurrentHashMap because it provides finer‑grained locking without locking the entire map.
10. Difference between IdentityHashMap and HashMap
IdentityHashMap uses reference equality ( ==) for keys, while HashMap uses logical equality via equals(). Consequently, two distinct objects that are equal according to equals() are considered different keys in an IdentityHashMap.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
