Fundamentals 11 min read

Key Java Collection Framework Interview Questions and Answers

This article provides concise English explanations for common Java collection interview questions, covering differences between HashMap and Hashtable, internal structures, thread‑safety, ArrayList vs LinkedList vs Vector, Array vs ArrayList, HashSet implementation, and the distinctions among List, Set, and Map.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Key Java Collection Framework Interview Questions and Answers

1. Difference between HashMap and Hashtable?

HashMap is not thread‑safe.

HashMap implements the Map interface, allowing null keys and values, while Hashtable does not permit nulls and synchronizes all its methods.

Hashtable is a thread‑safe collection.

Key differences:

HashMap allows null keys/values; Hashtable does not.

Hashtable inherits from the old Dictionary class; HashMap was introduced with the Map interface in Java 1.2.

Hashtable methods are synchronized; HashMap methods are not.

HashMap removed the ambiguous contains method in favor of containsKey and containsValue.

2. What is the underlying structure of HashMap?

HashMap consists of an array of Entry objects, each holding a key‑value pair. The overall structure is an array combined with linked lists (or trees in newer JDKs) to resolve hash collisions.

The array holds the primary buckets; each bucket may contain a linked list of entries when multiple keys hash to the same index. Fewer collisions mean better performance.

3. Why is HashMap not thread‑safe?

Because its internal operations (e.g., resizing, rehashing, and linked‑list updates) are not synchronized, concurrent modifications can lead to data corruption or infinite loops.

4. Difference between ArrayList and LinkedList?

ArrayList is based on a dynamic array; LinkedList is based on a doubly‑linked list.

Random access (get/set) is faster in ArrayList; LinkedList requires pointer traversal.

Insertion and removal are faster in LinkedList because it does not need to shift array elements.

5. Difference between ArrayList and Vector?

1. Synchronization:

Vector synchronizes all its methods, making it thread‑safe; ArrayList does not synchronize, offering higher performance in single‑threaded contexts.

Remember: Vector and Hashtable are legacy, thread‑safe collections; ArrayList and HashMap are newer, non‑thread‑safe alternatives.

2. Growth strategy:

Both start with an initial capacity and grow when needed. Vector typically doubles its capacity; ArrayList grows by about 1.5× (as seen in the source code's grow() method using newCapacity = oldCapacity + (oldCapacity >> 1)).

6. Difference between Array and ArrayList?

Arrays can hold primitive types and object references; ArrayList can only hold object references.

Array size is fixed; ArrayList can resize automatically.

ArrayList implements the List interface and provides many utility methods not available to raw arrays.

7. How does HashSet work?

HashSet is built on a HashMap with an initial capacity of 16 and a load factor of 0.75. Elements are stored as keys in the internal HashMap, with a constant dummy value (PRESENT). Proper equals() and hashCode() implementations are required for correct behavior.

8. When to choose HashMap vs TreeMap?

Refer to the linked article (issue 03) for detailed criteria: use HashMap for fast, unordered access; use TreeMap when a sorted order of keys is required.

9. Differences among List, Set, and Map?

List preserves insertion order, allows duplicates, and is typically implemented by ArrayList or LinkedList. Example: ArrayList uses a dynamic array and expands via grow() and Arrays.copyOf(); LinkedList uses a doubly‑linked list.

Set contains no duplicate elements and does not guarantee order (HashSet) or provides sorted order (TreeSet). HashSet stores elements as keys in an internal HashMap and expands roughly double when the load factor is exceeded.

Map stores key‑value pairs. Implementations include:

HashMap – hash‑table based, unsorted.

LinkedHashMap – maintains insertion order or access order (LRU).

TreeMap – red‑black tree based, sorted by keys, supports subMap().

10. How does HashMap resolve hash collisions?

See issue 16 for an in‑depth discussion. In brief, collisions are handled by chaining (linked lists) and, from Java 8 onward, by converting long chains into balanced trees for better performance.

Recent articles:

How to determine if an object is alive? (GC)

Dubbo interview eight‑question series

ZooKeeper interview topics

Instead of searching for questions online, follow us now!

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavainterviewHashMapCollectionsArrayList
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.