Register Login

Java Collection Interview Questions and Answer

Updated Jul 17, 2019

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 collection framework are:

  • The APU has a common set of interfaces like Map, Collection, Set, and List along with some methods. This reduces the time required for developing our 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, the Java objects were grouped together using HashTables, Arrays, and Vectors. There was no common interface to operate them and code had to be developed to work with all these collections. Therefore, the Collection framework was introduced to manage these collections with some core interfaces.

Why is the collection framework necessary for Java?

The Collection framework is necessary for Java to reduce the programmer’s effort so that he can focus on the task and not write unnecessary code. Additionally, operations like insertion, sorting, searching and deletion can also be performed.

How does the Collection framework work in Java?

The Collection framework has interfaces like Collection interface that is implemented by the classes in the framework. This interface has different methods like Boolean addAll and Boolean add which can be used by the subclasses. The Collection framework has a List interface that is implemented by classes such as Vector, ArrayList, Stack, and LinkedList.

How to import the collection framework in Java?

The Collection framework can be imported in a Java program by using the following code:

import java.util.collections.*;

This code will import the complete collections framework as specified by the * symbol.

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

In Java, a linked list can be created without using the collections framework by using three different classes. For example, in the program, one class can be called Node that will set the data and information about the next node. The other class can be called LinkedList that will be used for the different list operations like insert, delete, or add the node in different positions within the list. The final class can be used to take user input for the various list operations.

What are the interfaces in collection framework?

In Java, interfaces can have methods and variables but they will be abstract by default and will have no methods body. The class implementing an interface in the Collections framework will be able to use them for various functions. Example - List, Set, Queue, Dequeue, SortedSet, and SortedMap are the interfaces in Collection.

Explain the difference between ArrayList and LinkedList classes?

The differences between ArrayList and LinkedList classes in collection framework are:

ArrayList

LinkedList

It uses a dynamic array to store elements.

Here, a doubly-linked list is used for storing the elements.

This list is better for accessing and storing the data

The list is better for data manipulation

Data manipulation is slower here as the internal array is utilized. If an element is removed, the bits are moved to the memory  

Data manipulation is faster here as a doubly-linked list is used and no shifting is required

As this class implements the only List, it can work only as a list

As this class implements both Queue and List interfaces, it can work as a queue and a list  

Insertion is slower as its Index has to be updated if a node is inserted anywhere except the end

 

Insertion operations are faster here as there is no need for resizing arrays and copying data into new arrays if the array gets full

What are Generics in collection framework?

Generics were introduced in Java to handle type-safe objects. It is used to make the code management process efficient and finding the bugs during compilation time. With the feature, only a single type of object can be stored. Additionally, it removes the need for typecasting objects. The code for creating objects of the generic class is:

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

Generic functions can be called with a different type of arguments. The compiler handles the methods based on these arguments.

Explain the difference between HashSet and HashMap classes?

The difference between HashSet and HashMap classes in collection framework is:

HashSet

HashMap

It implements Set interface

It implements Map interface

No duplicate values are allowed and so duplicate elements are not allowed.

No duplicate keys are allowed here but duplicate values are allowed. In case a key is a duplicate, then the new value is swapped with the old key  

A single null value is allowed

It allows a single null key and there is no limit of null values

Here only one object is required for any operation

It requires two objects, a key and value to add an element

It uses add() method to add or store data

It uses put() method for storing data

Here, the add(object) argument works as the key. A dummy value is associated with each value passed as an argument in the method.  

There is no use of a dummy value

It is slower than HashMap

It works way faster than HashSet

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

The differences between Iterator and Enumeration interface in collection framework are:

Iterator

Enumeration

When it traverses the Collection object, it can remove an element using the remove() method.

When it traverses the Collections object, no modifications can be performed during the traversal.

There is no legacy interface here

It has a legacy interface that can be used to traverse legacy classes like Stack, Vector and HashTable

It is fail-fast in nature because it will throw a ConcurrentModificationException if the collection is modified during iteration with any method other than remove()

It is fail-safe in nature and no modifications are allowed during iteration

It has hasNext(), remove() and next() methods

It has hasMoreElements() and nextElement() methods

Why Map is not part of collection framework?

The Map interface is not a part of the collection as it is not compatible with the structure of the collection. As it requires only key-value pairs to work, it was not included in the collection framework interfaces that work with values only. For example, Collections like Set and Queue store a single value whereas Map stores only key-value pairs. There is an add(Object o) in Collection whose signature cannot be implemented with Map.            


×