Java Collections Framework (JCF) is a standard library that provides data structures and algorithms to store, retrieve and manipulate data efficiently. It is one of the most important topics in Java interviews.
This covers the most important Collection Frameworks & Generics interview questions in Java, explained in a clear, concise manner with examples.
1. What is Collection Framework in Java?
A Collection is a group of objects in Java. The collection framework is a set of interfaces and classes in Java that are used to represent and manipulate collections of objects in a variety of ways. The collection framework contains classes (ArrayList, Vector, LinkedList, PriorityQueue, TreeSet) and multiple interfaces (Set, List, Queue, Deque) where every interface is used to store a specific type of data.
2. Explain various interfaces used in the Collection framework.
Collection framework implements
- Collection Interface
- List Interface
- Set Interface
- Queue Interface
- Deque Interface
- Map Interface
Collection interface: Collection is the primary interface available that can be imported using java.util.Collection.
Syntax:
public interface Collection<E> extends Iterable<E>
3. How can you synchronize an ArrayList in Java?
An ArrayList can be synchronized using two methods mentioned below:
- Using Collections.synchronizedList()
- Using CopyOnWriteArrayList
Using Collections.synchronizedList():
public static List<T> synchronizedList(List<T> list)
Using CopyOnWriteArrayList:
- Create an empty List.
- It implements the List interface
- It is a thread-safe variant of ArrayList
- T represents generic
4. Why do we need a synchronized ArrayList when we have Vectors (which are synchronized) in Java?
- ArrayList is faster since it’s not synchronized by default.
- Synchronization can be added only when needed using Collections.synchronizedList().
- Vector is legacy, while ArrayList is the modern choice.
Example:
List<String> list = Collections.synchronizedList(new ArrayList<>());
5. Why can’t we create a generic array?
Generic arrays can't be created because an array carries type information of its elements at runtime because of which during runtime it throw 'ArrayStoreException' if the elements' type is not similar. Since generics type information gets erased at compile time by Type Erasure, the array store check would have been passed where it should have failed.
6. How are elements stored in memory for arrays and ArrayLists in Java?
Arrays: Elements are stored in contiguous memory, allowing direct access using index calculation (baseAddress + index * elementSize).
ArrayList: Also uses contiguous memory internally, but it’s dynamic. When capacity is full, a new larger array is created and elements are copied over. This lets ArrayLists grow or shrink as needed.
7. How to convert between ArrayList and Array in Java?
Conversion of List to ArrayList
There are multiple methods to convert List into ArrayList

Programmers can convert an Array to ArrayList using asList() method of the Arrays class. It is a static method of the Arrays class that accepts the List object.
Syntax:
Arrays.asList(item)
Example: Java program to demonstrate conversion of Array to ArrayList of fixed-size.
Java
import java.util.*;
// Driver Class
class GFG {
// Main Function
public static void main(String[] args)
{
String[] temp = { "Abc", "Def", "Ghi", "Jkl" };
// Conversion of array to ArrayList using Arrays.asList
List conv = Arrays.asList(temp);
System.out.println(conv);
}
}
Output[Abc, Def, Ghi, Jkl]
Conversion of ArrayList to Array:

Java programmers can convert ArrayList to
Syntax:
List_object.toArray(new String[List_object.size()])
Example:
Java
import java.util.List;
import java.util.ArrayList;
// Driver Class
class GFG {
// Main Function
public static void main(String[] args) {
// List declared
List<Integer> arr = new ArrayList<>();
arr.add(1);
arr.add(2);
arr.add(3);
arr.add(2);
arr.add(1);
// Conversion
Object[] objects = arr.toArray();
// Printing array of objects
for (Object obj : objects)
System.out.print(obj + " ");
}
}
8. How does the size of ArrayList grow dynamically? And also state how it is implemented internally.
An ArrayList in Java is backed by a dynamic array. Its default capacity is 10 (may vary by Java version). When the existing array becomes full and a new element is added, the ArrayList automatically creates a new array with larger capacity (usually 1.5 times the old size). All elements from the old array are then copied into the new one and the internal reference is updated to point to this new array. This process is called resizing.
9. What is a Vector in Java?
Vectors in Java are similar and can store multiple elements inside them. Vectors follow certain rules mentioned below:
- Vector can be imported using Java.util.Vector.
- Vector is implemented using a dynamic array as the size of the vector increases and decreases depending upon the elements inserted in it.
- Elements of the Vector using index numbers.
- Vectors are synchronized in nature means they only used a single thread ( only one process is performed at a particular time ).
- The vector contains many methods that are not part of the collections framework.
Syntax:
Vector gfg = new Vector(size, increment);
10. How to make Java ArrayList Read-Only?
An ArrayList can be made ready only using the method provided by Collections using the Collections.unmodifiableList() method.
Syntax:
array_readonly = Collections.unmodifiableList(ArrayList);
Example:
Java
import java.util.*;
public class Main {
public static void main(String[] argv) throws Exception {
try {
// creating object of ArrayList
ArrayList<Character> temp = new ArrayList<>();
// populate the list
temp.add('X');
temp.add('Y');
temp.add('Z');
// printing the list
System.out.println("Initial list: " + temp);
// getting readonly list using unmodifiableList() method
List<Character> new_array = Collections.unmodifiableList(temp);
// printing the list
System.out.println("ReadOnly ArrayList: " + new_array);
// Attempting to add element to new Collection
System.out.println("\nAttempting to add element to the ReadOnly ArrayList");
new_array.add('A');
} catch (UnsupportedOperationException e) {
System.out.println("Exception is thrown : " + e);
}
}
}
OutputInitial list: [X, Y, Z]
ReadOnly ArrayList: [X, Y, Z]
Attempting to add element to the ReadOnly ArrayList
Exception is thrown : java.lang.UnsupportedOperationException
11. What is a priority queue in Java?

A priority queue is an abstract data type similar to a regular queue or stack data structure. Elements stored in elements are depending upon the priority defined from low to high. The PriorityQueue is based on the priority heap.
Syntax:
Java
import java.util.*;
class PriorityQueueDemo {
// Main Method
public static void main(String args[]) {
// Creating empty priority queue
PriorityQueue<Integer> var1 = new PriorityQueue<Integer>();
// Adding items to the pQueue using add()
var1.add(10);
var1.add(20);
var1.add(15);
// Printing the top element of PriorityQueue
System.out.println(var1.peek());
}
}
12. Explain the LinkedList class.
The LinkedList class in Java is a collection class that uses a doubly linked list to store elements. It inherits the AbstractSequentialList class and implements the List and Deque interfaces. Properties of the LinkedList Class are mentioned below:
- LinkedList classes are non-synchronized.
- Maintains insertion order.
- It can be used as a list, stack or queue.
Syntax:
LinkedList<class> list_name=new LinkedList<class>();
13. What is the Stack class in Java and what are the various methods provided by it?
A Stack class in Java is a LIFO data structure that implements the Last In First Out data structure. It is derived from a Vector class but has functions specific to stacks. The Stack class in java provides the following methods:
- peek(): returns the top item from the stack without removing it
- empty(): returns true if the stack is empty and false otherwise
- push(): pushes an item onto the top of the stack
- pop(): removes and returns the top item from the stack
- search(): returns the 1, based position of the object from the top of the stack. If the object is not in the stack, it returns -1
14. What is Set in the Java Collections framework and list down its various implementations?
Sets are collections that don't store duplicate elements. They don't keep any order of the elements. The Java Collections framework provides several implementations of the Set interface, including:
- HashSet: HashSet in Java, stores the elements in a has table which provides faster lookups and faster insertion. HashSet is not ordered.
- LinkedHashSet: LinkedHashSet is an implementation of HashSet which maintains the insertion order of the elements.
- TreeSet: TreeSet stores the elements in a sorted order that is determined by the natural ordering of the elements or by a custom comparator provided at the time of creation.
15. What is the HashSet class in Java and how does it store elements?
The HashSet class implements the Set interface in the Java Collections Framework and is a member of the HashSet class. Unlike duplicate values, it stores a collection of distinct elements. In this implementation, each element is mapped to an index in an array using a hash function and the index is used to quickly access the element. It produces an index for the element in the array where it is stored based on the input element. Assuming the hash function distributes the elements among the buckets appropriately, the HashSet class provides constant-time performance for basic operations (add, remove, contain and size).
16. What is LinkedHashSet in Java Collections Framework?
The LinkedHashSet is an ordered version of Hashset maintained by a doubly-linked List across all the elements. It is very helpful when iteration order is needed. During Iteration in LinkedHashSet, elements are returned in the same order they are inserted.
Syntax:
LinkedHashSet<E> hs = new LinkedHashSet<E>();
Example:
Java
import java.io.*;
import java.util.*;
// Driver Class
class GFG {
// Main Function
public static void main(String[] args) {
// LinkedHashSet declared
LinkedHashSet<Integer> hs = new LinkedHashSet<Integer>();
// Add elements in HashSet
hs.add(1);
hs.add(2);
hs.add(5);
hs.add(3);
// Print values
System.out.println("Values:" + hs);
}
}
OutputValues:[1, 2, 5, 3]
17. What is a Map interface in Java?

The map interface is present in the Java collection and can be used with Java.util package. A map interface is used for mapping values in the form of a key-value form. The map contains all unique keys. Also, it provides methods associated with it like containsKey(), contains value (), etc.
There are multiple types of maps in the map interface as mentioned below:
- SortedMap
- TreeMap
- HashMap
- LinkedHashMap
18. Explain Treemap in Java
TreeMap is a type of map that stores data in the form of key-value pair. It is implemented using the red-black tree. Features of TreeMap are :
- It contains only unique elements.
- It cannot have a NULL key
- It can have multiple NULL values.
- It is non-synchronized.
- It maintains ascending order.
19. What is EnumSet?
EnumSet is a specialized implementation of the Set interface for use with enumeration type. A few features of EnumSet are:
- It is non-synchronized.
- Faster than HashSet.
- All of the elements in an EnumSet must come from a single enumeration type.
- It doesn't allow null Objects and throws NullPointerException for exceptions.
- It uses a fail-safe iterator.
Syntax:
public abstract class EnumSet<E extends Enum<E>>
Parameter: E specifies the elements.
20. What is BlockingQueue?

A blocking queue is a Queue that supports the operations that wait for the queue to become non-empty while retrieving and removing the element and wait for space to become available in the queue while adding the element.
Syntax:
public interface BlockingQueue<E> extends Queue<E>
Parameters: E is the type of elements stored in the Collection
21. What is ConcurrentHashMap in Java and how do you implement it?
ConcurrentHashMap is a thread-safe implementation of the Map interface. Unlike HashMap, it allows concurrent read and write operations without synchronizing the whole map.
Syntax:
ConcurrentHashMap<K, V> map = new ConcurrentHashMap<>();
Example:
Java
map.put(1, "Amit");
map.put(2, "Rahul");
22. Can you use any class as a Map key?
Yes, any class can be used as a Map key if it follows these rules:
- The class must override both equals() and hashCode() methods properly.
- Keys should be immutable for reliable behavior.
- null cannot be used as a key in HashMap (before Java 8 only one null key was allowed, not in ConcurrentHashMap).
23. What is an Iterator?

The Iterator interface provides methods to iterate over any Collection in Java. Iterator is the replacement of Enumeration in the Java Collections Framework. It can get an iterator instance from a Collection using the _iterator()_ method. It also allows the caller to remove elements from the underlying collection during the iteration.
24. What is the difference between Collection and Collections?
Collection | Collections |
---|
The Collection is an Interface. | Collections is a class. |
It provides the standard functionality of data structure. | It is to sort and synchronize the collection elements. |
It provides the methods that can be used for the data structure. | It provides static methods that can be used for various operations. |
25. Differentiate between Array and ArrayList in Java.
Array | ArrayList |
---|
Single-dimensional or multidimensional | Single-dimensional |
For and for each used for iteration | Here iterator is used to traverse riverArrayList |
length keyword returns the size of the array. | size() method is used to compute the size of ArrayList. |
The array has Fixed-size. | ArrayList size is dynamic and can be increased or decreased in size when required. |
It is faster as above we see it of fixed size | It is relatively slower because of its dynamic nature |
Primitive data types can be stored directly in unlikely objects. | Primitive data types are not directly added to unlikely arrays, they are added indirectly with help of autoboxing and unboxing |
They can not be added here hence the type is in the unsafe. | They can be added here hence makingArrayList type-safe. |
The assignment operator only serves the purpose | Here a special method is used known as add() method |
26. What is the difference between Array and Collection in Java?
Array | Collections |
---|
Array in Java has a fixed size. | Collections in Java have dynamic sizes. |
In an Array, Elements are stored in contiguous memory locations. | In Collections, Elements are not necessarily stored in contiguous memory locations. |
Objects and primitive data types can be stored in an array. | We can only store objects in collections. |
Manual manipulation is required for resizing the array. | Resizing in collections is handled automatically. |
The array has basic methods for manipulation. | Collections have advanced methods for manipulation and iteration. |
The array is available since the beginning of Java. | Collections were introduced in Java 1.2. |
27. Difference between ArrayList and LinkedList.
ArrayList | LinkedList |
---|
ArrayList is Implemented as an expandable Array. | LinkedList is Implemented as a doubly-linked list. |
In ArrayList, Elements are stored in contiguous memory locations | LinkedList Elements are stored in non-contiguous memory locations as each element has a reference to the next and previous elements. |
ArrayLists are faster for random access. | LinkedLists are faster for insertion and deletion operations |
ArrayLists are more memory efficient. | LinkedList is less memory efficient |
ArrayLists Use more memory due to maintaining the array size. | LinkedList Uses less memory as it only has references to elements |
The search operation is faster in ArrayList. | The search operation is slower in LinkedList |
28. Differentiate between ArrayList and Vector in Java.
ArrayList | Vector |
---|
ArrayList is implemented as a resizable array. | Vector is implemented as a synchronized, resizable array. |
ArrayList is not synchronized. | The vector is synchronized. |
ArrayLists are Faster for non-concurrent operations. | Vector is Slower for non-concurrent operations due to added overhead of synchronization. |
ArrayLists were Introduced in Java 1.2. | Vector was Introduced in JDK 1.0. |
Recommended for use in a single-threaded environment. | Vectors are Recommended for use in a multi-threaded environment. |
The default initial capacity of ArrayLists is 10. | In Vectors, the default initial capacity is 10 but the default increment is twice the size. |
ArrayList performance is high. | Vector performance is low. |
29. What is the difference between Iterator and ListIterator?
Iterator | ListIterator |
---|
Can traverse elements present in Collection only in the forward direction. | Can traverse elements present in Collection both in forward and backward directions. |
Used to traverse Map, List and Set. | Can only traverse List and not the other two. |
Indexes can't be obtained using Iterator | It has methods like nextIndex() and previousIndex() to obtain indexes of elements at any time while traversing the List. |
Can't modify or replace elements present in Collection | Can modify or replace elements with the help of set(E e) |
Can't add elements and also throws ConcurrentModificationException. | Can easily add elements to a collection at any time. |
Certain methods of Iterator are next(), remove() and hasNext(). | Certain methods of ListIterator are next(), previous(), hasNext(), hasPrevious(), add(E e). |
30. Differentiate between HashMap and HashTable.
HashMap | HashTable |
---|
HashMap is not synchronized | HashTable is synchronized |
One key can be a NULL value | NULL values not allowed |
The iterator is used to traverse HashMap. | Both Iterator and Enumertor can be used |
HashMap is faster. | HashTable is slower as compared to HashMap. |
31. What is the difference between Iterator and Enumeration?
Iterator | Enumeration |
---|
The Iterator can traverse both legacies as well as non-legacy elements. | Enumeration can traverse only legacy elements. |
The Iterator is fail-fast. | Enumeration is not fail-fast. |
The Iterators are slower. | Enumeration is faster. |
The Iterator can perform a remove operation while traversing the collection. | The Enumeration can perform only traverse operations on the collection. |
32. What is the difference between Comparable and Comparator?
Comparable | Comparator |
---|
The interface is present in java.lang package. | The Interface is present in java.util package. |
Provides compareTo() method to sort elements. | Provides compare() method to sort elements. |
It provides single sorting sequences. | It provides multiple sorting sequences. |
The logic of sorting must be in the same class whose object you are going to sort. | The logic of sorting should be in a separate class to write different sorting based on different attributes of objects. |
Method sorts the data according to fixed sorting order. | Method sorts the data according to the customized sorting order. |
It affects the original class. | It doesn't affect the original class. |
Implemented frequently in the API by Calendar, Wrapper classes, Date and String. | It is implemented to sort instances of third-party classes. |
33. What is the difference between Set and Map?
Set | Map |
---|
The Set interface is implemented using java.util package. | The map is implemented using java.util package. |
It can extend the collection interface. | It does not extend the collection interface. |
It does not allow duplicate values. | It allows duplicate values. |
The set can sort only one null value (in HashSet/LinkedHashSet). | The map can sort multiple null values. |
34. Explain the FailFast iterator and FailSafe iterator along with examples for each.
A FailFast iterator is an iterator that throws a ConcurrentModificationException if it detects that the underlying collection has been modified while the iterator is being used. This is the default behavior of iterators in the Java Collections Framework. For example, the iterator for a HashMap is FailFast.
Example:
Java
import java.io.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
class GFG {
public static void main(String[] args) {
HashMap<Integer, String> map = new HashMap<>();
map.put(1, "one");
map.put(2, "two");
Iterator<Map.Entry<Integer, String>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<Integer, String> entry = iterator.next();
// this will throw a ConcurrentModificationException
if (entry.getKey() == 1) {
map.remove(1);
}
}
}
}
Output:
Exception in thread "main" java.util.ConcurrentModificationException
A FailSafe iterator does not throw a ConcurrentModificationException if the underlying collection is modified while the iterator is being used. Alternatively, it creates a snapshot of the collection at the time the iterator is created and iterates over the snapshot. For example, the iterator for a ConcurrentHashMap is FailSafe.
Example:
Java
import java.io.*;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
class GFG {
public static void main(String[] args) {
ConcurrentHashMap<Integer, String> map = new ConcurrentHashMap<>();
map.put(1, "one");
map.put(2, "two");
Iterator<Map.Entry<Integer, String>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<Integer, String> entry = iterator.next();
// this will not throw an exception
if (entry.getKey() == 1) {
map.remove(1);
}
}
}
}
Explore
Java Basics
OOP & Interfaces
Collections
Exception Handling
Java Advanced
Practice Java