Java Collections – Top 50 Interview Questions & Answers

 

Java Collections – Top 50 Interview Questions & Answers


1️⃣ What is the Java Collection Framework?

A unified architecture to store, retrieve, and manipulate groups of objects using interfaces like List, Set, and Map.


2️⃣ Why do we need collections?

To handle dynamic data, reduce coding effort, improve performance, and provide reusable data structures.


3️⃣ What are the main interfaces in the Collection Framework?

  • List

  • Set

  • Queue

  • Deque

  • Map (part of framework but not a child of Collection)


4️⃣ Difference between Collection and Collections?

CollectionCollections
InterfaceUtility class
Represents dataHelper methods

5️⃣ Difference between List, Set, and Map?

Type특징
ListAllows duplicates, ordered
SetNo duplicates
MapKey–value pairs

6️⃣ What is an ArrayList?

A resizable array implementation of List.


7️⃣ Difference between ArrayList and LinkedList?

ArrayListLinkedList
Fast readFast insert/delete
Uses arrayUses doubly linked list

8️⃣ What is Vector?

A legacy synchronized list (rarely used now).


9️⃣ Difference between ArrayList and Vector?

ArrayListVector
Not synchronizedSynchronized
FasterSlower

🔟 What is a Set?

A collection that does not allow duplicates.


1️⃣1️⃣ Difference between HashSet and TreeSet?

HashSetTreeSet
UnorderedSorted
FasterSlower

1️⃣2️⃣ What is LinkedHashSet?

Maintains insertion order while preventing duplicates.


1️⃣3️⃣ How does HashSet work internally?

Uses a HashMap internally to store elements as keys.


1️⃣4️⃣ What is a Map?

Stores data in key–value pairs.


1️⃣5️⃣ Difference between HashMap and Hashtable?

HashMapHashtable
Not synchronizedSynchronized
Allows nullNo null

1️⃣6️⃣ Difference between HashMap and LinkedHashMap?

HashMapLinkedHashMap
No orderMaintains insertion order

1️⃣7️⃣ Difference between HashMap and TreeMap?

HashMapTreeMap
UnorderedSorted
FasterSlower

1️⃣8️⃣ How does HashMap work internally?

Uses hashing, buckets, and linked lists / red-black trees (Java 8+).


1️⃣9️⃣ What is load factor?

Determines when a HashMap resizes (default = 0.75).


2️⃣0️⃣ What happens when HashMap is full?

It resizes and rehashes entries.


2️⃣1️⃣ What is collision in HashMap?

When multiple keys produce the same hash index.


2️⃣2️⃣ How are collisions handled?

Using:

  • Linked list (Java 7)

  • Linked list → Red-Black Tree (Java 8+)


2️⃣3️⃣ What is ConcurrentHashMap?

A thread-safe, high-performance Map.


2️⃣4️⃣ Difference between HashMap and ConcurrentHashMap?

HashMapConcurrentHashMap
Not thread-safeThread-safe
Allows nullNo null

2️⃣5️⃣ Why ConcurrentHashMap is faster than Hashtable?

Uses bucket-level locking instead of full synchronization.


2️⃣6️⃣ What is Queue?

A collection for holding elements before processing (FIFO).


2️⃣7️⃣ What is PriorityQueue?

Queue where elements are ordered by priority.


2️⃣8️⃣ Difference between Queue and Deque?

QueueDeque
One-end operationsBoth ends

2️⃣9️⃣ What is ArrayDeque?

A resizable array implementation of Deque.


3️⃣0️⃣ What is an Iterator?

Used to traverse a collection safely.


3️⃣1️⃣ Difference between Iterator and ListIterator?

IteratorListIterator
Forward onlyBidirectional
All collectionsList only

3️⃣2️⃣ What is fail-fast iterator?

Throws ConcurrentModificationException if collection is modified.


3️⃣3️⃣ What is fail-safe iterator?

Works on a copy of the collection (no exception).


3️⃣4️⃣ Example of fail-safe collection?

CopyOnWriteArrayList


3️⃣5️⃣ What is Collections.sort()?

Utility method to sort a list.


3️⃣6️⃣ Difference between Comparable and Comparator?

ComparableComparator
Natural orderCustom order
compareTo()compare()

3️⃣7️⃣ Can we sort a Map?

Directly ❌
Convert to:

  • List<Map.Entry>

  • or use TreeMap


3️⃣8️⃣ What is immutability in collections?

Unmodifiable collections cannot be changed.


3️⃣9️⃣ How to create immutable collections?

List.of("A", "B");

4️⃣0️⃣ What is EnumMap?

A Map optimized for enum keys.


4️⃣1️⃣ What is WeakHashMap?

Keys are weakly referenced → garbage collected when unused.


4️⃣2️⃣ What is IdentityHashMap?

Uses == instead of equals() for key comparison.


4️⃣3️⃣ What is SynchronizedMap?

Thread-safe wrapper using Collections.synchronizedMap().


4️⃣4️⃣ Difference between synchronizedMap and ConcurrentHashMap?

synchronizedMapConcurrentHashMap
Single lockSegment/bucket locking
SlowerFaster

4️⃣5️⃣ What is CopyOnWriteArrayList?

Thread-safe list optimized for read-heavy scenarios.


4️⃣6️⃣ What is NavigableMap?

Provides navigation methods like lowerKey(), higherKey().


4️⃣7️⃣ What is Spliterator?

Supports parallel traversal of collections.


4️⃣8️⃣ What is Stream API’s relation to collections?

Streams process collections in a functional style.


4️⃣9️⃣ Why Map is not part of Collection interface?

Because Map works with key–value pairs, not single elements.


5️⃣0️⃣ When to use which collection?

ScenarioUse
Fast lookupHashMap
Ordered dataLinkedHashMap
Sorted dataTreeMap
Thread safetyConcurrentHashMap

🎯 Interview Final Tip

Most collection questions test internal working and trade-offs, not just definitions.

No comments:

Post a Comment

Java Collections – Top 50 Interview Questions & Answers

  Java Collections – Top 50 Interview Questions & Answers 1️⃣ What is the Java Collection Framework? A unified architecture to store...

Featured Posts