Register Login

Java Collection Interview Questions and Answer

Updated Apr 28, 2025

What is collection framework in Java?

In Java, a collection is a group of similar objects represented as a unit. The Java Collection framework has several classes and interfaces that are used to group these objects. The java.util.Collection and the java.util.Map are the two primary interfaces of the Collection framework.

What are the advantages of collection framework?

The benefits of the collection framework are:

  • The API has a common set of interfaces like Map, Collection, Set, and List along with standard methods. This reduces the time required for developing classes.
  • As the classes are included with the JDK, the time needed for code maintenance is reduced.
  • The quality of code is enhanced as the classes have been well-tested.

Why is the use of the collection framework in Java?

Earlier, Java objects were grouped together using HashTables, Arrays, and Vectors. There was no common interface to operate them, and custom code had to be written to work with each. The Collection framework was introduced to unify and manage these collections with some core interfaces.

Why is the collection framework necessary for Java?

The Collection framework reduces the programmer’s effort by providing ready-to-use interfaces and classes for operations like insertion, sorting, searching, and deletion. This way, developers can focus on solving business problems rather than writing boilerplate code.

How does the Collection framework work in Java?

The Collection framework has core interfaces like Collection, implemented by classes such as ArrayList, LinkedList, Vector, and Stack. These interfaces provide methods like add() and addAll() that are used for various operations on groups of objects.

How to import the collection framework in Java?

The Collection framework can be imported using:

import java.util.*;

This statement imports all the classes and interfaces inside java.util.

How to make a linked list in Java without using the collection framework?

To create a linked list without using the collection framework:

  • Create a Node class to store data and a reference to the next node.
  • Create a LinkedList class to manage operations like insertion, deletion, and display.
  • Use a separate main or user interface class to interact with the list.

What are the interfaces in collection framework?

In Java, interfaces are abstract by default. In the Collection framework, important interfaces include:

  • List
  • Set
  • Queue
  • Deque
  • SortedSet
  • SortedMap

Explain the difference between ArrayList and LinkedList classes

ArrayList LinkedList
Uses a dynamic array to store elements. Uses a doubly-linked list to store elements.
Better for storing and accessing data. Better for manipulating data (insertions, deletions).
Slower for data manipulation as shifting is required. Faster for data manipulation without shifting.
Implements only the List interface. Implements both List and Deque interfaces.
Insertion in the middle is slower due to index updates. Insertion is faster as no shifting is required.

What are Generics in collection framework?

Generics were introduced to provide type safety and remove the need for typecasting. They allow classes and methods to operate on objects of various types while providing compile-time type safety. Example:

BaseType<Type> obj = new BaseType<Type>();

Explain the difference between HashSet and HashMap classes

HashSet HashMap
Implements Set interface. Implements Map interface.
No duplicate elements allowed. No duplicate keys allowed, but duplicate values are allowed.
Allows one null element. Allows one null key and multiple null values.
Only one object (the element) is required. Two objects (key and value) are required.
Uses add() method to add elements. Uses put() method to store key-value pairs.
Generally slower than HashMap. Faster than HashSet.

What is the difference between Iterator and Enumeration interface in collection framework?

Iterator Enumeration
Can remove elements while traversing using remove() method. Cannot modify elements while traversing.
Does not use a legacy interface. Uses a legacy interface for legacy classes like Vector and Stack.
Fail-fast: throws ConcurrentModificationException if modified concurrently. Fail-safe: no concurrent modification allowed.
Methods: hasNext(), next(), remove(). Methods: hasMoreElements(), nextElement().

Why is Map not part of collection framework?

Map is not a part of the Collection interface because it works on key-value pairs and does not fit the single-object structure of collections like List or Set. Collections store only values, whereas Maps store keys along with values, thus making them structurally different.

What is the difference between Collection and Collections in Java?

Collection is an interface that represents a group of objects known as elements, whereas Collections is a utility class that provides static methods to operate on collections, such as sorting, searching, and synchronizing them.

What is the difference between Iterator and ListIterator in Java?

Iterator ListIterator
Can traverse only in a forward direction. Can traverse in both forward and backward directions.
Works with all collections like Set, List, and Queue. Works only with List types.
Provides methods: hasNext(), next(), remove(). Provides additional methods: hasPrevious(), previous(), add(), set().

What is the difference between fail-fast and fail-safe iterators in Java?

Fail-fast iterators immediately throw a ConcurrentModificationException if the collection is modified structurally while iterating. Examples: ArrayList, HashMap iterator.
Fail-safe iterators work on a cloned copy of the collection and do not throw exceptions during concurrent modifications. Examples: CopyOnWriteArrayList, ConcurrentHashMap iterator.

What is ConcurrentHashMap and how is it different from HashMap?

ConcurrentHashMap is part of java.util.concurrent package and allows concurrent access from multiple threads without locking the entire map. In contrast, HashMap is not thread-safe and must be synchronized externally if multiple threads access it concurrently.

What is the NavigableMap interface?

The NavigableMap interface extends SortedMap to provide navigation methods such as lowerEntry(), floorEntry(), ceilingEntry(), and higherEntry(). TreeMap is a concrete class that implements NavigableMap.

What is the difference between TreeSet and TreeMap?

TreeSet TreeMap
Implements Set interface and stores only unique elements. Implements Map interface and stores key-value pairs.
Elements are stored in a sorted order according to their natural ordering or a comparator. Keys are stored in a sorted order according to their natural ordering or a comparator.
Uses add() method to add elements. Uses put() method to store key-value pairs.


×