34 Java Collections Interview Questions
In Java, collection interview questions are most asked by the interviewers. Here is the list of the most asked collections interview questions with answers.
1) What is the Collection framework in Java?
Collection Framework is a combination of classes and interface, which is used to store and manipulate the data in the form of objects. It provides various classes such as ArrayList, Vector, Stack, and HashSet, etc. and interfaces such as List, Queue, Set, etc. for this purpose.
2) What are the main differences between array and collection?
Array and Collection are somewhat similar regarding storing the references of objects and manipulating the data, but they differ in many ways. The main differences between the array and Collection are defined below:
3) Explain various interfaces used in Collection framework?
Collection framework implements various interfaces, Collection interface and Map interface (java.util.Map) are the mainly used interfaces of Java Collection Framework. List of interfaces of Collection Framework is given below:
1. Collection interface: Collection (java.util.Collection) is the primary interface, and every collection must implement this interface.
Where <E> represents that this interface is of Generic type
2. List interface: List interface extends the Collection interface, and it is an ordered collection of objects. It contains duplicate elements. It also allows random access of elements.
3. Set interface: Set (java.util.Set) interface is a collection which cannot contain duplicate elements. It can only include inherited methods of Collection interface
Queue interface: Queue (java.util.Queue) interface defines queue data structure, which stores the elements in the form FIFO (first in first out).
4. Dequeue interface: it is a double-ended-queue. It allows the insertion and removal of elements from both ends. It implants the properties of both Stack and queue so it can perform LIFO (Last in first out) stack and FIFO (first in first out) queue, operations.
5. Map interface: A Map (java.util.Map) represents a key, value pair storage of elements. Map interface does not implement the Collection interface. It can only contain a unique key but can have duplicate elements. There are two interfaces which implement Map in java that are Map interface and Sorted Map.
4) What is the difference between ArrayList and Vector?
5) What is the difference between ArrayList and LinkedList?
6) What is the difference between Iterator and ListIterator?
Iterator traverses the elements in the forward direction only whereas ListIterator traverses the elements into forward and backward direction.
7) What is the difference between Iterator and Enumeration?
8) What is the difference between List and Set?
The List and Set both extend the collection interface. However, there are some differences between the both which are listed below.
9) What is the difference between HashSet and TreeSet?
The HashSet and TreeSet, both classes, implement Set interface. The differences between the both are listed below.
10) What is the difference between Set and Map?
The differences between the Set and Map are given below.
11) What is the difference between HashSet and HashMap?
The differences between the HashSet and HashMap are listed below.
12) What is the difference between HashMap and TreeMap?
The differences between the HashMap and TreeMap are given below.
13) What is the difference between HashMap and Hashtable?
14) What is the difference between Collection and Collections?
The differences between the Collection and Collections are given below.
15) What is the difference between Comparable and Comparator?
16) What do you understand by BlockingQueue?
BlockingQueue is an interface which extends the Queue interface. It provides concurrency in the operations like retrieval, insertion, deletion. While retrieval of any element, it waits for the queue to be non-empty. While storing the elements, it waits for the available space. BlockingQueue cannot contain null elements, and implementation of BlockingQueue is thread-safe.
17) What is the advantage of Properties file?
If you change the value in the properties file, you don't need to recompile the java class. So, it makes the application easy to manage. It is used to store information which is to be changed frequently. Consider the following example.
18) What does the hashCode() method?
The hashCode() method returns a hash code value (an integer number).
The hashCode() method returns the same integer number if two keys (by calling equals() method) are identical.
However, it is possible that two hash code numbers can have different or the same keys.
If two objects do not produce an equal result by using the equals() method, then the hashcode() method will provide the different integer result for both the objects.
19) Why we override equals() method?
The equals method is used to check whether two objects are the same or not. It needs to be overridden if we want to check the objects based on the property.
For example, Employee is a class that has 3 data members: id, name, and salary. However, we want to check the equality of employee object by the salary. Then, we need to override the equals() method.
20) How to synchronize List, Set and Map elements?
Yes, Collections class provides methods to make List, Set or Map elements as synchronized:
21) What is the advantage of the generic collection?
There are three main advantages of using the generic collection.
22) What is hash-collision in Hashtable and how it is handled in Java?
Two different keys with the same hash value are known as hash-collision. Two separate entries will be kept in a single hash bucket to avoid the collision. There are two ways to avoid hash-collision.
23) What is the Dictionary class?
The Dictionary class provides the capability to store key-value pairs.
24) What is the default size of load factor in hashing based collection?
The default size of load factor is 0.75. The default capacity is computed as initial capacity * load factor. For example, 16 * 0.75 = 12. So, 12 is the default capacity of Map.
25) What do you understand by fail-fast?
The Iterator in java which immediately throws ConcurrentmodificationException, if any structural modification occurs in, is called as a Fail-fast iterator. Fail-fats iterator does not require any extra space in memory.
The main differences between the Array and ArrayList are given below.
The length of an array can be obtained using the property of length whereas ArrayList does not support length property, but we can use size() method to get the number of objects in the list.
Finding the length of the array
Finding the size of the ArrayList
We can convert an Array to ArrayList by using the asList() method of Arrays class. asList() method is the static method of Arrays class and accepts the List object. Consider the following syntax:
We can convert an ArrayList to Array using toArray() method of the ArrayList class. Consider the following syntax to convert the ArrayList to the List object.
We can obtain java ArrayList Read-only by calling the Collections.unmodifiableCollection() method. When we define an ArrayList as Read-only then we cannot perform any modification in the collection through add(), remove() or set() method.
There are two ways to remove duplicates from the ArrayList.
The Process to remove duplicate elements from ArrayList using the LinkedHashSet:
To reverse an ArrayList, we can use reverse() method of Collections class. Consider the following example.
printing the list.... 10 50 30 printing list in reverse order.... 30 50 10
To sort the ArrayList in descending order, we can use the reverseOrder method of Collections class. Consider the following example.
printing the list.... 10 50 30 60 20 90 printing list in descending order.... 90 60 50 30 20 10
We can synchronize ArrayList in two ways.
LinkedLists are better to use for the update operations whereas ArrayLists are better to use for the search operations.