Top 50+ Java Collections Interview Questions (2024)
Top 50+ Java Collections Interview Questions (2024)
Top 50+ Java Collections Interview Questions (2024)
Answers
Last Updated : 15 Jun, 2024
Java Collection Framework was introduced in JDK 1.2 which contains all
the collection classes and interfaces. Java Collection is a framework that
provides a mechanism to store and manipulate the collection of objects. It
allows developers to access prepackaged data structures and algorithms for
manipulating data.
Here, we’ve covered the 50+ Java Collections Interview Questions and
Answers tailored for both Fresher and experienced professionals, which
cover everything from basic to advanced Java collection concepts such as
navigation collection, WeakHashMap, streams Lambdas, etc.
Table of Content
https://www.geeksforgeeks.org/java-collections-interview-questions/?ref=lbp 1/44
6/24/24, 11:14 PM Top 50+ Java Collections Interview Questions (2024)
https://www.geeksforgeeks.org/java-collections-interview-questions/?ref=lbp 2/44
other implementations. Some of the popular frameworks in java are:
6/24/24, 11:14 PM Top 50+ Java Collections Interview Questions (2024)
Spring
Hibernate
Struts
Google Web Toolkit (GWT)
JavaServer Faces (JSF)
Arrays Collection
Arrays are fixed in size that is once we The collection is growable in nature
create an array we can not increase or and is based on our requirements.
decrease based on our requirements. We can increase or decrease of size.
https://www.geeksforgeeks.org/java-collections-interview-questions/?ref=lbp 3/44
represent groups of objects known as elements. The java platform does not
provide any direct implementation
6/24/24, 11:14 PM Top 50+of this
Java interface
Collections Interviewbut the (2024)
Questions Collection
interface is being implemented by List and Set classes.
Collection interface
List interface
Set interface
Queue interface
Dequeue interface
Map interface
https://www.geeksforgeeks.org/java-collections-interview-questions/?ref=lbp 4/44
Consistent API: The API has a basic set of interfaces like Collection, Set,
6/24/24, 11:14 PM Top 50+ Java Collections Interview Questions (2024)
List, or Map, all the classes (ArrayList, LinkedList, Vector, etc) that
implement these interfaces have some common set of methods.
https://www.geeksforgeeks.org/java-collections-interview-questions/?ref=lbp 5/44
6/24/24, 11:14 PM Top 50+ Java Collections Interview Questions (2024)
Collection Collections
https://www.geeksforgeeks.org/java-collections-interview-questions/?ref=lbp 6/44
6/24/24, 11:14 PM Top 50+ Java Collections Interview Questions (2024)
ArrayList LinkedList
This class works better when the This class works better when the
application demands storing the data application demands manipulation of
and accessing it. the stored data.
https://www.geeksforgeeks.org/java-collections-interview-questions/?ref=lbp 7/44
6/24/24, 11:14 PM Top 50+ Java Collections Interview Questions (2024)
Syntax:
Note: Here “c” is any Collection object. itr is of type Iterator interface and
refers to “c”.
https://www.geeksforgeeks.org/java-collections-interview-questions/?ref=lbp 8/44
12. What is the difference between List and Set in Java
6/24/24, 11:14 PM Top 50+ Java Collections Interview Questions (2024)
A major difference between a List and a Set is that a List can contain
duplicate elements while a set contains only unique elements. The list is
Ordered and maintains the order of the object to which they are added. The
set is unordered.
List Set
Multiple null elements can be stored. Null elements can store only once.
For more information, refer to the article – Difference Between List and
Set in Java
13. What are the best practices for Java Collections Framework?
Following are some of the best practices while using Java Collections:
https://www.geeksforgeeks.org/java-collections-interview-questions/?ref=lbp 9/44
Set is the best way to avoid duplicates.
6/24/24, 11:14 PMUse immutable classes provided by JDK
Top 50+ Java as Interview
Collections keys in Map (2024)
Questions to avoid
implementation of hashCode() and equals().
In order to increase the readability of the code, we should use isEmpty()
instead of finding the size of the collection and comparing it to zero.
Rather than writing your own implementation, use the Collections utility
class to get read-only, Synchronized, or empty collections instead. It
enhances code reuse while resulting in greater stability.
The elements of the priority queue are ordered according to the natural
ordering, or by a Comparator provided at queue construction time,
depending on which constructor is used.
Declaration:
https://www.geeksforgeeks.org/java-collections-interview-questions/?ref=lbp 10/44
Queue<E> interfaces.
6/24/24, 11:14 PM Top 50+ Java Collections Interview Questions (2024)
15. What is the difference between List, set, and map in java?
https://www.geeksforgeeks.org/java-collections-interview-questions/?ref=lbp 11/44
which means that the element which means that the element inserted
6/24/24, 11:14 PM Top 50+ Java Collections Interview Questions (2024)
inserted at the last will be the first first will be the first element that will be
element that will be taken out. taken out.
The BlockingQueue interface in Java is added in Java 1.5 along with various
other concurrent Utility classes like ConcurrentHashMap, Counting
Semaphore, CopyOnWriteArrrayList, etc. The BlockingQueue interface
supports flow control (in addition to queue) by introducing blocking if either
BlockingQueue is full or empty.
Usage of BlockingQueue
https://www.geeksforgeeks.org/java-collections-interview-questions/?ref=lbp 12/44
6/24/24, 11:14 PM Top 50+ Java Collections Interview Questions (2024)
Declaration:
https://www.geeksforgeeks.org/java-collections-interview-questions/?ref=lbp 13/44
6/24/24, 11:14 PM Top 50+ Java Collections Interview Questions (2024)
Syntax :
https://www.geeksforgeeks.org/java-collections-interview-questions/?ref=lbp 14/44
6/24/24, 11:14 PM Top 50+ Java Collections Interview Questions (2024)
ArrayList Vector
Iterator ListIterator
https://www.geeksforgeeks.org/java-collections-interview-questions/?ref=lbp 15/44
6/24/24, 11:14 PM Helps to traverse Map, List, Top 50+ Java Collections Interview Questions (2024)
Can only traverse List and not the other two.
and Set.
Syntax:
Syntax:
https://www.geeksforgeeks.org/java-collections-interview-questions/?ref=lbp 16/44
6/24/24, 11:14 PM Top 50+ Java Collections Interview Questions (2024)
Iterator Enumeration
The iterator has the remove() Enumeration does not have the
method. remove() method.
https://www.geeksforgeeks.org/java-collections-interview-questions/?ref=lbp 17/44
6/24/24, 11:14 PM Top 50+ Java Collections Interview Questions (2024)
HashMap in Java
Syntax:
For Example, the HashSet class implements the Set interface which is a
subinterface of the Collection interface. If a collection implementation
https://www.geeksforgeeks.org/java-collections-interview-questions/?ref=lbp 18/44
6/24/24, 11:14 PM Top 50+ Java Collections Interview Questions (2024)
In Java, the List interface allows the user to store an ordered collection of
objects. The list is the child interface of Collection. In Collection, a list is an
ordered collection of objects which can have duplicate values. Since List
preserves the insertion order, it allows positional access and insertion, which
also allows duplicate values.
Syntax:
https://www.geeksforgeeks.org/java-collections-interview-questions/?ref=lbp 19/44
Stack, etc. Since all the subclasses implement the list, we can instantiate a
list object with any of these classes.
6/24/24, 11:14 PM Top 50+ Java Collections Interview Questions (2024)
Example:
ArrayList
LinkedList
Vector
Stack
25. Write a program to convert a given array into a collection with the
asList() method.
https://www.geeksforgeeks.org/java-collections-interview-questions/?ref=lbp 20/44
// import java util library
6/24/24, 11:14 PM import java.util.*; Top 50+ Java Collections Interview Questions (2024)
Output
HashSet HashMap
https://www.geeksforgeeks.org/java-collections-interview-questions/?ref=lbp 21/44
HashSet. HashMap.
6/24/24, 11:14 PM Top 50+ Java Collections Interview Questions (2024)
HashSet HashTable
https://www.geeksforgeeks.org/java-collections-interview-questions/?ref=lbp 22/44
externally. Synchronized.
6/24/24, 11:14 PM Top 50+ Java Collections Interview Questions (2024)
28. What is the default size of the load factor in the hashing-based
collection?
As the Load Factor increases, the capacity increases so that the operational
complexity of the HashMap remains O(1) if the ratio of the current element
to the initial capacity crosses the threshold. The meaning of operational
complexity of O(1) means the retrieval and insertion operations take
constant time. The default load factor size is 0.75. The default capacity is
calculated by multiplying the initial capacity by the load factor.
Java provides two interfaces to sort objects using data members of the
class:
Comparable
Comparator
Comparable Comparator
https://www.geeksforgeeks.org/java-collections-interview-questions/?ref=lbp 23/44
comparable interface Comparator interface.
6/24/24, 11:14 PM Top 50+ Java Collections Interview Questions (2024)
Iterators in Java are used to iterate over the Collection objects. Fail-Fast
iterators immediately throw ConcurrentModificationException if there is a
structural modification of the collection. Structural modification means
adding, or removing any element from a collection while a thread is iterating
over that collection. Iterator on ArrayList and HashMap classes are some
examples of fail-fast Iterator.
Fail-Fast
Fail-Safe
ConcurrentModificationException is
thrown while modifying the object during No Exception is thrown
the iteration process.
https://www.geeksforgeeks.org/java-collections-interview-questions/?ref=lbp 24/44
6/24/24, 11:14 PM Top 50+ Java Collections Interview Questions (2024)
Fail-Fast does not allow modification Fail-Safe allows modification
during the process of iteration. during the process of iteration.
Examples:
Examples:
ConcurrentHashMap,
ArrayList, Vector, HashMap, HashSet, etc.
CopyOnWriteArrayList, etc.
For more information, refer to the article – Fail Fast and Fail Safe
Iterators in Java
31. Write a program to iterate the list using the lambda expression.
Syntax:
list_name.forEach(variable->{//block of code})
Java
// Class
class GFG {
https://www.geeksforgeeks.org/java-collections-interview-questions/?ref=lbp 25/44
l.add("for");
6/24/24, 11:14 PM l.add("Geeks"); Top 50+ Java Collections Interview Questions (2024)
Output
Geeks
for
Geeks
For more information, refer to the article – Iterate through List in Java
The hashtable class implements a hash table, which maps keys to values.
Any non-null object can be used as a key or as a value. To successfully store
and retrieve objects from a hashtable, the objects used as keys must
implement the hashCode method and the equals method. Below is the
program to display the contents of a HashTable using enumeration:
https://www.geeksforgeeks.org/java-collections-interview-questions/?ref=lbp 26/44
// as an Enumeration of Hashtable class
6/24/24, 11:14 PM Top 50+ Java Collections Interview Questions (2024)
import java.io.*;
import java.util.Enumeration;
import java.util.Hashtable;
// Main class
// EnumerationOnKeys
public class GFG {
Output
Geeks
for
Geeks
https://www.geeksforgeeks.org/java-collections-interview-questions/?ref=lbp 27/44
Java s HashMap class has the java.util.HashMap.values() method for creating
collections out of HashMap values.
6/24/24, 11:14 PM Top 50+ It basically
Java returns
Collections Interview a Collection
Questions (2024) view of
HashMap values.
Java
Output
For more information, refer to the article – HashMap values() Method in Java
35. Write a program to join two ArrayList into one single ArrayList.
https://www.geeksforgeeks.org/java-collections-interview-questions/?ref=lbp 28/44
6/24/24, 11:14 PM // Java program to demonstrate
Top 50+ Java Collections Interview Questions (2024)
// How to join ArrayList
import java.util.*;
list_1.add("Geeks");
list_1.add("For");
list_1.add("ForGeeks");
list_2.add("GeeksForGeeks");
list_2.add("A computer portal");
Output
For more information, refer to the article – Join two ArrayLists in Java
https://www.geeksforgeeks.org/java-collections-interview-questions/?ref=lbp 29/44
collections in Java. SynchronizedList() returns a synchronized (thread safe)
list backed by a selection.
6/24/24, 11:14 PM Top 50+ Java Collections Interview Questions (2024)
Java
class GFG {
public static void main(String[] args)
{
// Non Synchronized ArrayList
List<String> list = new ArrayList<String>();
list.add("Eat");
list.add("Coffee");
list.add("Code");
list.add("Sleep");
list.add("Repeat");
Output
Eat
Coffee
Code
Sleep
Repeat
https://www.geeksforgeeks.org/java-collections-interview-questions/?ref=lbp 30/44
a list of values whose key is a string and whose value is also a string.
Properties can define other properties
6/24/24, 11:14 PM Top 50+ Javaclass lists,
Collections butQuestions
Interview the default
(2024) is
properties.
https://www.geeksforgeeks.org/java-collections-interview-questions/?ref=lbp 31/44
6/24/24, 11:14 PM Top 50+ Java Collections Interview Questions (2024)
39. What will happen if two different keys of HashMap return the same
hashcode()?
When two different keys of HashMap return the same hash code, they will
end up in the same bucket; therefore, collisions will occur. n case of collision,
i.e. index of two or more nodes is the same, nodes are joined by a link list i.e.
the second node is referenced by the first node and the third by the second,
and so on.
Syntax:
https://www.geeksforgeeks.org/java-collections-interview-questions/?ref=lbp 32/44
6/24/24, 11:14 PM Top 50+ Java Collections Interview Questions (2024)
For more information, refer to the article –
UnsupportedOperationException
PriorityQueue TreeSet
Except for the root element, the In TreeSet all the elements remain in the
rest of the elements do not follow sorted order.
https://www.geeksforgeeks.org/java-collections-interview-questions/?ref=lbp 33/44
PriorityQueue.
6/24/24, 11:14 PM Top 50+ Java Collections Interview Questions (2024)
Diamond operators are used for simplifying the use of generics when
creating objects while avoiding unchecked warnings in a program. When the
Diamond operator was introduced in Java 7, we can create the object
without mentioning the generic type on the right side of the expression as
shown below.
Syntax:
TreeMap stores the key-value pairs, but TreeMap sorts the keys ascending
rather than descending like HashMap. Depending on which constructor is
used, TreeMap will be sorted either based on its keys, or by a Comparator. In
TreeMap, the elements are sorted based on a Red-Black tree. A red-black
tree is a self-balancing binary search tree where each node has an extra bit,
and that bit is often interpreted as the color (red or black). These colors are
https://www.geeksforgeeks.org/java-collections-interview-questions/?ref=lbp 34/44
6/24/24, 11:14 PM Top 50+ Java Collections Interview Questions (2024)
The HashMap class provides Java’s Map interface by storing data in (Key,
Value) pairs and accessing them by an index of another type. To use this
class it is necessary to import java.util.HashMap package or its superclass.
There are numerous ways to iterate over HashMap of which 5 are listed
below:
https://www.geeksforgeeks.org/java-collections-interview-questions/?ref=lbp 35/44
6/24/24, 11:14 PM Top 50+ Java Collections Interview Questions (2024)
CopyOnWriteArrayList in Java
Syntax:
https://www.geeksforgeeks.org/java-collections-interview-questions/?ref=lbp 36/44
Parameters:
6/24/24, 11:14 PM Top 50+ Java Collections Interview Questions (2024)
EnumMap in Java
int hash
K key
V value
Node next
Hashing
Buckets
Index Calculation in Hashmap
https://www.geeksforgeeks.org/java-collections-interview-questions/?ref=lbp 37/44
6/24/24, 11:14 PM Top 50+ Java Collections Interview Questions (2024)
Example:
Java
import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
Iterator<Integer> it = arr.iterator();
while (it.hasNext()) {
if (it.next() == 2) {
// will not throw Exception
it.remove();
}
}
System.out.println(arr);
https://www.geeksforgeeks.org/java-collections-interview-questions/?ref=lbp 38/44
arr.remove(3);
6/24/24, 11:14 PM } Top 50+ Java Collections Interview Questions (2024)
}
}
}
Output:
[1, 3, 4, 5]
Exception in thread "main" java.util.ConcurrentModificationException
at
java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
at java.util.ArrayList$Itr.next(ArrayList.java:851)
at FailFastExample.main(FailFastExample.java:28)
Conclusion
Java Collections is important to understand for Java developers or
programmers because Java is widely used in various industries. It’s
important for developers to have a solid understanding of core concepts of
Java Collections. Java is one of the most widely used languages in top
companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify,
Amazon, etc. To get into these companies or any other IT companies, you
need to master these mostly asked Java Collections interview questions in
order to crack their Java-based online assessment and technical interview.
https://www.geeksforgeeks.org/java-collections-interview-questions/?ref=lbp 39/44
6/24/24, 11:14 PM Top 50+ Java Collections Interview Questions (2024)
There are many collections in Java but out of them most used
collections are:
1. ArrayList
2. LinkedList
3. HashSet
4. Stack
https://www.geeksforgeeks.org/java-collections-interview-questions/?ref=lbp 40/44
exciting journey to master backend development efficiently and on schedule.
What We Offer:
6/24/24, 11:14 PM Top 50+ Java Collections Interview Questions (2024)
Comprehensive Course
Expert Guidance for Efficient Learning
Hands-on Experience with Real-world Projects
Proven Track Record with 100,000+ Successful Geeks
GeeksforGeeks 32
Similar Reads
Difference between Traditional Collections and Concurrent Collections i…
We all know about Traditional Collections ( i.e. List, Set, Queue and its
implemented Classes) and Concurrent Collection (i.e. ConcurrentMap interfac…
3 min read
https://www.geeksforgeeks.org/java-collections-interview-questions/?ref=lbp 41/44
Java is one of the most popular programming languages in the world, known
6/24/24, 11:14 for
PM its versatility, portability, and wide
Top range
50+ Java of applications.
Collections Interview Questions Java
(2024) is the most…
Company Explore
About Us Job-A-Thon Hiring Challenge
Legal Hack-A-Thon
Careers GfG Weekly Contest
In Media Offline Classes (Delhi/NCR)
Contact Us DSA in JAVA/C++
Advertise with us Master System Design
https://www.geeksforgeeks.org/java-collections-interview-questions/?ref=lbp 42/44
Python Data Structures
6/24/24, 11:14 PM Java Top 50+ Java Collections Interview QuestionsAlgorithms
(2024)
https://www.geeksforgeeks.org/java-collections-interview-questions/?ref=lbp 43/44
Biology Management
6/24/24, 11:14 PM
Social Science HR Management
Top 50+ Java Collections Interview Questions (2024)
English Grammar Finance
Income Tax
https://www.geeksforgeeks.org/java-collections-interview-questions/?ref=lbp 44/44