0% found this document useful (0 votes)
3 views3 pages

Java Collections Interview QA

The document provides an overview of various Java collections, focusing on their internal workings, differences, and use cases. Key topics include HashMap, ConcurrentHashMap, ArrayList, and their performance characteristics in multi-threaded environments. It also discusses memory management, iterator types, and how to implement custom collections and caching mechanisms.

Uploaded by

pal.sushma2201
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Java Collections Interview QA

The document provides an overview of various Java collections, focusing on their internal workings, differences, and use cases. Key topics include HashMap, ConcurrentHashMap, ArrayList, and their performance characteristics in multi-threaded environments. It also discusses memory management, iterator types, and how to implement custom collections and caching mechanisms.

Uploaded by

pal.sushma2201
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Q: How does HashMap work internally?

A: HashMap stores entries in buckets based on hashCode(). Collisions are handled via linked lists,
and from Java 8, deep buckets convert into Red-Black trees for O(log n) worst-case lookup.

Q: Difference between HashMap, ConcurrentHashMap, and Hashtable?


A: HashMap is non-thread-safe. Hashtable is thread-safe but locks entire map.
ConcurrentHashMap uses bucket-level locking (Java 7) or CAS (Java 8) for better concurrency.

Q: When does HashMap switch from LinkedList to Red-Black Tree? Why?


A: When a bucket exceeds 8 elements, it converts to a Red-Black tree in Java 8 to improve lookup
from O(n) to O(log n).

Q: How does ArrayList grow internally?


A: ArrayList resizes by 1.5x when capacity is exceeded, using Arrays.copyOf(). This amortizes
insert cost to O(1).

Q: Explain CopyOnWriteArrayList.
A: It creates a new copy of the array on every write. Ideal for read-heavy, write-light scenarios. Fast
reads but memory-heavy and slow writes.

Q: Difference between fail-fast and fail-safe iterators?


A: Fail-fast iterators (ArrayList, HashMap) throw ConcurrentModificationException. Fail-safe
iterators (CopyOnWriteArrayList, ConcurrentHashMap) work on snapshots and don’t fail.

Q: How does ConcurrentHashMap achieve thread safety?


A: Java 7 used segmented locks, Java 8 uses CAS and node-level locks, enabling concurrent reads
and writes.

Q: Why should keys in HashMap be immutable?


A: Immutable keys prevent hashCode/equals changes after insertion, avoiding lookup issues and
memory leaks.

Q: When to use LinkedList over ArrayList?


A: LinkedList is better for frequent middle insert/delete. ArrayList is better for random access and
cache locality.

Q: How does WeakHashMap help in avoiding memory leaks?


A: Keys are weakly referenced, so GC can reclaim them when no strong references exist, useful for
caching.

Q: How do you implement an LRU cache using collections?


A: Extend LinkedHashMap with accessOrder=true and override removeEldestEntry(). Provides
O(1) get/put with LRU removal.

Q: Which collection for high reads and fewer writes in multi-threaded environment?
A: CopyOnWriteArrayList (for lists) and ConcurrentHashMap (for maps). Ideal when reads
dominate writes.

Q: What is the difference between BlockingQueue implementations?


A: ArrayBlockingQueue is bounded with array, LinkedBlockingQueue is node-based,
PriorityBlockingQueue orders by priority.

Q: Difference between PriorityQueue and TreeSet?


A: PriorityQueue allows duplicates and is optimized for polling min/max. TreeSet disallows
duplicates and supports range queries.

Q: When to use EnumMap?


A: EnumMap is array-based, very fast, and memory-efficient for enum keys, maintaining natural
order.

Q: Collections.synchronizedList() vs CopyOnWriteArrayList?
A: SynchronizedList locks on every operation, iteration needs external sync. CopyOnWriteArrayList
uses snapshot iteration, faster for reads but costly for writes.

Q: How does BlockingQueue work internally? Where is it used?


A: It blocks producers if full and consumers if empty. Used in producer-consumer, thread pools, and
messaging.

Q: Difference between ConcurrentSkipListMap and ConcurrentHashMap?


A: SkipListMap is sorted with O(log n), ConcurrentHashMap is hash-based with O(1) average but
unordered.

Q: How do memory leaks occur in HashMap with mutable keys?


A: If key’s hashCode/equals changes after insertion, it becomes unreachable in the map, causing
leaks.

Q: Why prefer immutable keys in maps?


A: They guarantee stable hashCode/equals and thread-safety, preventing lookup failures.

Q: WeakHashMap vs IdentityHashMap?
A: WeakHashMap uses weak references for keys (GC-friendly). IdentityHashMap uses reference
equality (==) for keys.

Q: How to implement an LRU cache efficiently?


A: Extend LinkedHashMap with accessOrder=true and override removeEldestEntry(). Widely used
in caching systems.

Q: Thread-safe list with more reads than writes?


A: CopyOnWriteArrayList – lock-free iteration and safe reads, costly writes due to array copy.

Q: Maintain insertion order with fast lookup?


A: LinkedHashMap (or LinkedHashSet). Maintains insertion order and provides O(1) lookup.

Q: How to implement a custom collection?


A: Extend AbstractList or AbstractMap and implement core methods like get/size/put. Other
behaviors are inherited.

You might also like