Java Collections Interview Questions

Collections Framework forms the backbone of Java. Therefore, Java Collections interview questions hold immense importance for a career in Java. 

According to Oracle’s Java documentation, Collections Framework is a unified architecture for representing and manipulating collections, enabling collections to be manipulated independently of implementation details. 

We have divided the Java Collections interview questions into five sections. 

Collections Framework: 

What is the Collections Framework in Java and its advantages? 

The Collections framework is a set of classes and interfaces with defined architecture, where the algorithms or methods are already defined. Programmers just need to use them and there is no need to write code for any of them.  

Hence collections framework consists of:

  • Interfaces 
  • Classes
  • Algorithms/methods 

The advantages of  Collections Framework in Java are: 

  • Better performance: The in-built algorithms, data structures, and methods are optimized for high-performance. 
  • Less effort: Reduces overall effort for writing, designing, implementing, and learning APIs. Because most of the methods across classes are similar, this reduces the effort. 
  • Reduced programming time: Since, the data structures, algorithms, and their methods are already written in Java, programmers’ time is reduced considerably. 
  • Reusability: Everyone can use the same set of standard collections and their algorithms which enhances reusability. 

Explain the Collections Framework hierarchy in Java. 

This is one of the basic questions from Java Collections interview questions. Knowledge of the framework hierarchy provides clarity of the interfaces and classes present in it. 

Package java.util contains all the classes and interfaces of the collections framework.  

Below is the collections framework hierarchy in Java where:

  • Green box signifies an interface 
  • Orange box signifies a class which implements the interface above it. 
Hierarchy of Collections Framework
Image from techvidvan.com

The components of the hierarchy are:

1. Iterable interface

2. Collection Interface

List interface

  • ArrayList class 
  • LinkedList class 
  • Vector class 
  • Stack class 

3. Queue interface 

  • Priority queue

4. Set interface 

  • HashSet class 
  • LinkedHashSet class 

5. Deque interface 

  • ArrayDeque class 

6. SortedSet interface 

  • TreeSet class 

7. Map interface

  • HashTable class
  • LinkedHashMap class
  • HashMap class
  • SortedMap interface : TreeMap class 

Name and explain various interfaces of the Collections framework.

 The core collection interfaces form the foundation of the collections framework. 

  • Collection: Collection interface is at the root of the hierarchy, providing access to the group of objects. 
  • List: List interface extends collection interface in Java. Its object stores an ordered collection of elements which need not be unique. 
  • Queue: Holds multiple elements before processing, usually it is treated FIFO(First-in, First-out). 
  • Dequeue: Holds multiple elements before processing like List. Dequeue can be FIFO or LIFO(Last-in, First-out). Insertion, removal, and retrieval of elements are from both ends. 
  • Set: Extends collection interface and contains unique elements. 
  • SortedSet: Extends set interface and stores unique elements in sorted order. 
  • Map: Object of Map interface maps the keys to values. 
  • SortedMap: A map that stores mapping in ascending order of keys. E.g. dictionary 

List

What is a List interface in Java? Name the classes which implement it. 

  • The List is a child interface of the Collection interface in Java. 
  • It stores the elements in order(sequence). 
  • Duplicate elements are allowed.
  • The List is an interface hence we cannot create objects of this. We can use a class that extends List and then create an object of that class. 
  • Operations performed on List interface in Java are- creation, positional access, search, iteration, etc. 

Classes which extend List interface in Java are:

  • ArrayList 
  • Vector 
  • Stack 
  • LinkedList 

What is an ArrayList in Java 

ArrayList is a class in the java.util package. The built-in array of Java has a fixed size while ArrayList is a dynamic array. Below is the syntax to create a new ArrayList in Java: 

Methods available in ArrayList are:

  • add() 
  • get() 
  • 2 2
  • set()
  • remove() 
  • clear() 
  • size() 
  • sort() 

More about ArrayList here. 

What is the difference between Array and ArrayList? 

How to reverse the list? 

1. We can use java.util.Collections.reverse() method to reverse an ArrayList. 

2. To reverse a LinkedList same code can be used just replace ArrayList definition with  ‘List mylist = new LinkedList();’ 

3. Array does not have a reverse() method. We can use the same reverse method to reverse an Array. 

 

Set

What is a Set interface in Java and its various implementations? 

  • The Set interface extends the Collection interface and provides an implementation of a mathematical set. 
  • A set cannot contain duplicate elements. 
  • The elements in a set are not ordered. 
  • Since it extends the Collection interface, the Set interface has methods inherited from the Collection interface. 
  • One additional method is to disallow the insertion of duplicate elements. 
  • Set is an interface hence we can not create objects of it. We need to use the classes which implement the set interface. 

The various implementations of Set interface in Java are: 

a. EnumSet
b. HashSet
c. LinkedHashSet
d. TreeSet 

Implementations of Set interface 
Image frim cdn.programiz.com

Example to demonstrate Set declaration and usage: 

What are the differences between Set and Queue in Java? 

To learn more click: Set and Queue. 

How HashSet Collection in Java store elements? 

HashSet is backed up by HashMap to store its elements. Whenever we create a HashSet object, one corresponding HashMap object also gets created. Hence, the elements which we enter into HashSet are actually entered into HashMap objects. Wherein the value of the key is stored as a constant – ’PRESENT’. 

Whenever a HashSet constructor is used in the code, the constructor internally creates a HashMap object.  

Below is an example and pictorial depiction of how HashSet stores the elements. 

Diagram below shows how a single add() method of HashSet creates a new HashMap object internally. It shows the contents of an internal HashMap object which has ‘Key’ and ‘Value’ pairs. 

Internal working of HashSet code
Image from i2.wp.com

What are the differences between HashSet and TreeSet? 

What is a LinkedHashSet in Set interface? Give an example. 

  • LinkedHashSet inherits HashSet and extends Set interface.  
  • Like HashSet its elements are unique. 
  • It may contain NULL elements. 
  • LinkedHashSet maintains a linked list and preserves the order of element insertion. 
  • While traversing the LinkedHashSet the elements are returned in the order they were inserted. 

Below is the code snippet for creating a LinkedHashSet object. 

Map

What is a Map interface in Java? 

The Map interface is present in the java.util package and is not related to the Collection interface. Hence, its behaviour is different from the Collection interface.

An object of the Map interface stores a key-value pair called entry. The key value is unique and is useful in searching, deleting, and updating an element. The value can be a duplicate. 

Operations on Map interface:

  1. Basic operations– put, get, remove, containsKeycontainsValue, size, and empty 
  2. Bulk operations–   putAll and clear 
  3. Collection View operations– keySetentrySet, and values 

More on Map interface here. 

Below is the Map interface hierarchy diagram:

Map interface hierarchy in Java
Image from static.javatpoint.com

What are the Collection views in Java by the Map interface? 

Although the Map interface is a part of the Collections Framework, it is different from the collection interface. Therefore, it provides Collection views to view the Map object’s elements in the form of Collection.  

There are three methods for this purpose: 

  • keySet()- Returns a Set view of keys present in the Map. 
  • values()- Returns a Collection view of values present in the Map. 
  • values()- Returns a Collection view of values present in the Map.

How will you design a good key for HashMap in Java? 

HashMap object stores a key-value pair. Key is processed by a hash function to provide a hash code. This hash-code acts as an index to the buckets or locations where the actual value is stored.  

During lookup, the key is hashed and the resulting hash shows the location where the actual value is stored. 

This is depicted in the diagram below:

Working of a hash table
Image from upload.wikimedia.org

The key-value pairs are stored in the memory with hashing. And then if we change the key, the hash-code will change for lookup. Therefore, the new hash-code will not match the actual hash-code and the value will be lost. Hence, we should use a key that is immutable e.g. String class. Because the objects of the String class cannot be changed once created. Therefore, they are the best candidate for the key. 

Explain the differences between HashMap and TreeMap.

Queue

What is a Queue interface in Java? Explain its methods.

Queue interface provides an implementation of the Queue data structure. It extends the Collection interface. The Queue is an interface so we cannot directly create objects of it.  

Therefore, there are three classes of Queue interface in Java:

  1. ArrayDeque 
  2. LinkedList 
  3. PriorityQueue 
Queue data structure
Image from cdn.programiz.com

Methods of Queue interface are:

  • add(): Inserts element into the queue at the tail. 
  • peek(): Retrieves the element at the head of the queue. Returns NULL if empty queue.
  • remove(): Removes the element at the head of the queue. 
  • element(): Checks if there are elements in the queue. Throws an exception if the queue is empty.poll(): Returns and removes the head of the queue. 
  • size(): Returns the number of elements in the queue. 

What are the differences between Queue and Deque? 

 

Related Articles

Responses