Java - Collections - Interview Questions

Java programming language provides numerous kinds of data structures and packages them within the Collections API. Java collections interview questions are very frequently asked both in telephonic screening interviews as well as in face-to-face interviews.

Your knowledge of the different Java collections provided by Java programming API, the difference between the different collections, when to use which collection and the efficiency of the collections - will be tested, either directly or indirectly, in most Java programming interviews. The interview can be for any level - beginner or senior; and for any role - Software engineer, Software programmer, Software architect or for Software management roles.

Knowledge in inner workings of different Java collections is critical for coding and algorithmic interviews. In most Java coding interview questions, you will end up using one or more of the Java collections for your solution.

Below Java collections interview questions, answers, tips and samples will get you grounded on the fundamentals; helps you to memorize some important methods, and review the basic operations that you will frequently perform on collections such as sorting, insertion and deletion.

Important keywords are provided at the end of the questions. Review them, make them a part of your Java collections vocabulary, and talk about them confidently during your interview process.

What do you understand by collections framework in Java programming language?

 FAQ
* Similar Java Interview Question Recently Asked @ Accenture, Altimetrik, Amdocs, Arm, Birst, Capgemini, Capita, Clover Infotech, Exusia, Fiserv, Headstrong, IBM, InterGlobe Technologies, ITC Infotech, Lucernex, Luxoft, MagikMinds, Maxxton, Naaptol, New York Times, Nisum, Nucleus Software, PwC, redBus, Reliance Jio Infocomm, Resilinc, Sapient, SJM Technologies, Softcell Technologies, UST Global, Zoho,

A collection is an object that groups or stores multiple objects of similar types. The objects that are stored in the collection are also called as elements of the collection.

Collections framework in Java programming language consists of the following.

1. Interfaces that represent different types of collections.
2. Concrete implementations of the collection interfaces.
3. Algorithmic implementations that perform useful computations, sorting, searching etc. on objects implementing the collection interfaces.

What are the key interfaces defined in Java collections framework?

 FAQ

Java collections framework includes the following key interfaces.

1. Collection - java.util.Collection interface is the root of the Java collections framework hierarchy. All other core collection interfaces in the Java collections framework, except for maps, extend from the java.util.Collection interface either directly or indirectly.

2. Set - java.util.Set represents a collection of unique elements. A set cannot contain duplicate elements. A set can contain one null element.

7. SortedSet - java.util.SortedSet extends from Set interface, and maintains its elements in ascending order. The sorting is done according to the natural order of the elements. If a comparator is provided, then sorting is done according to the comparator.

3. List - java.util.List represents an ordered collection of elements. Lists are ordered based on their index position. List can contain duplicate elements. Elements in a list can be accessed, inserted or deleted by their index position.

4. Queue - java.util.Queue represents a collection that is typically ordered in a FIFO (first-in first-out) manner. i.e. an element which is first put into the queue will be the first to be removed when a call to remove or poll is made. In a FIFO queue, new elements are inserted to the tail of the queue and elements are removed from the head of the queue.

5. Dequeue - java.util.Deque represent collections that can be either in a FIFO manner or in a LIFO manner. In dequeues elements can be inserted, retrieved and removed from both ends of the queue.

6. Map - java.util.Map represents a collection object that maps keys to values. A map cannot contain duplicate keys, and each key can map only to one value. Unlike other core collection interfaces, Map does not extend from the Collection interface.

8. SortedMap - java.util.SortedMap extends from Map interface, and maintains its elements in ascending order. The sorting is done according to the natural order of the keys. Or, if a comparator is provided, then sorting is done according to the comparator on the keys.

What are the core implementation classes of Set interface defined in Java collections framework?

 FAQ

Java collections framework provides classes HashSet, TreeSet, LinkedTreeSet, EnumSet and CopyOnWriteArray; which are implementations of java.util.Set interface.

HashSet

  • Construction - HashSet is an implementation of the Set interface backed by a hash table.
  • Iteration Order - The iteration order in a HashSet is not guaranteed.
  • null elements - A null element is permitted in a HashSet.
  • Performance - Constant time for basic operations of add(), remove(), contains() and size(). Iteration requires time proportional to sum of the HashSet size and capacity of the backing HashMap.
  • Synchronization - HashSet is not synchronized.
  • Iteration - Iterators returned by HashSet are fail-fast, i.e. if the set is modified after the iterator is created, then the Iterator throws a ConcurrentModificationException.

TreeSet

  • Construction - TreeSet is an implementation of the Set interface backed by a TreeMap.
  • Ordering -The elements in a TreeSet are ordered using the natural ordering of elements. If a comparator is provided then the ordering is done as per the comparator.
  • Performance - The TreeSet takes log(n) times for basic operations of add(), remove(), size() and contains().
  • TreeSet is not synchronized.
  • Iteration - Iterators returned by HashSet are fail-fast, i.e. if the set is modified after the iterator is created, then the Iterator throws a ConcurrentModificationException.

LinkedHashSet

  • LinkedHashSet is an implementation of the Set interface backed by a HashMap and a LinkedList, and maintains a doubly-linked list running through all of its entries.
  • Ordering - The elements in a TreeSet are ordered using the insertion order of elements.
  • Performance - The TreeSet takes log(n) times for basic operations of add, remove, size and contains.
  • Synchronization - LinkedHashSet is not synchronized.
  • Iteration - Iterators returned by HashSet are fail-fast, i.e. if the set is modified after the iterator is created, then the Iterator throws a ConcurrentModificationException.

What are the core implementation classes of List interface defined in Java collections framework?

 FAQ

Java collections framework provides classes ArrayList, LinkedList and CopyOnWriteArrayList; which are implementations of the List interface.

ArrayList

  • Construction - ArrayList is an implementation of the List interface that has the functionality of a re-sizable array.
  • Iteration Order - Elements in an ArrayList are ordered according to its index position.
  • null elements - null elements are permitted in ArrayList.
  • Performance - Constant time for basic operations of add(), remove(), contains() and size().
  • Synchronization - Methods in ArrayList are not synchronized.
  • Iteration - Iterators returned by HashSet are fail-fast, i.e. if the set is modified after the iterator is created, then the Iterator throws a ConcurrentModificationException.

LinkedList

  • Construction - LinkedList is a doubly-linked list implementation of the List interface.
  • Iteration Order - Elements in an LinkedList are ordered according to their insertion position.
  • null elements - null elements are permitted in ArrayList.
  • Performance - Constant time for basic operations of add(), remove(), contains() and size().
  • Synchronization - Methods in ArrayList are not synchronized.
  • Iteration - Iterators returned by ArrayList are fail-fast, i.e. if the set is modified after the iterator is created, then the Iterator throws a ConcurrentModificationException.

What are the core implementation classes of Map interface defined in Java collections framework?

 FAQ

Java collections framework provides classes HashMap, TreeMap and LinkedHashMap; which are implementations of java.util.Map interface.

HashMap

  • Construction - HashSet is an implementation of the Map interface which maintains a generic key value pairs.
    Iteration Order - The iteration order in a HashMap is not guaranteed.
  • null elements - A null element is permitted in a HashMap.
  • Performance - Constant time for basic operations of add(), remove(), contains() and size(). Iteration requires time proportional to sum of the HashMap size and capacity of the backing HashMap.
  • Synchronization - HashMap is not synchronized.
  • Iteration - Iterators returned by HashMap are fail-fast, i.e. if the set is modified after the iterator is created, then the Iterator throws a ConcurrentModificationException.

TreeMap

  • Construction - TreeMap is an implementation of the Map interface which maintains specific order of elements.
  • Ordering -The elements in a TreeMap are ordered using the natural ordering of elements. If a comparator is provided then the ordering is done as per the comparator.
  • Performance - The TreeMap takes log(n) times for basic operations of add(), remove(), size() and contains().
  • TreeMap is not synchronized.
  • Iteration- Iterators returned by HashMap are fail-fast, i.e. if the map is modified after the iterator is created, then the Iterator throws a ConcurrentModificationException.

LinkedHashMap

    LinkedHashMap is an implementation of the Map interface backed by a doubly-linked list running through all of its entries.
  • Ordering - The elements in a TreeMap are ordered using the insertion order of elements.
  • Performance - The TreeMap takes log(n) times for basic operations of add, remove, size and contains.
  • Synchronization - LinkedHashMap is not synchronized.
  • Iteration - Iterators returned by HashMap are fail-fast, i.e. if the map is modified after the iterator is created, then the Iterator throws a ConcurrentModificationException.

Java Interview Guide has over 250 REAL questions from REAL interviews. Get the guide for $15.00 only.
 
BUY EBOOK
 

What are the core implementation classes of Queue interface defined in Java collections framework?

 FAQ

Java collections framework provides classes LinkedList and PriorityQueue; which are implementations of the Queue interface. In addition the java.util.concurrent package provides classes LinkedBlockingQueue, ArrayBlockingQueue, PriorityBlockingQueue, DelayQueue, SynchronousQueue...

*** See complete answer and code snippet in the Java Interview Guide.

What are the core implementation classes of Deque interface defined in Java collections framework?

 FAQ

Java collections framework provides classes LinkedList and ArrayDeque; which are implementations of the Deque interface. In addition the java.util.concurrent package provides the class LinkedBlockingDeque which extends from the Deque interface...

*** See complete answer and code snippet in the Java Interview Guide.

What are the different wrapper classes that act on collections?

 FAQ

Java programming language provides wrapper functionality that adds additional functionality on top of that of collection classes. Wrapper functionality follows the decorator design pattern. Implementations of these wrapper methods are defined in the java.utils.Collections class.

Three main categories of wrappers are provided in the java.utils.Collections class. Synchronization wrappers, Unmodifiable wrappers and Checked Interface wrappers...

*** See complete answer and code snippet in the Java Interview Guide.

 
Java Interview Guide

$15.00

BUY EBOOK
  SSL Secure Payment
Java Interview Quesiuons - Secure Payment
Java Interview Guide

$15.00

BUY EBOOK
  SSL Secure Payment
Java Interview Quesiuons - Secure Payment
 

Java - Interview Questions

Java - Object Oriented ProgrammingJava - Objects & ClassesJava - Data TypesJava - VariablesJava - StringsJava - ArraysJava - CollectionsJava - ReflectionJava - Lambda ExpressionsJava - StreamsJava - GenericsJava - ExceptionsJava - IOJava - ThreadsJava - ConcurrencyJava - JDBCJava - NetworkingJava - SecurityJava - JVM InternalsJava - PerformanceJava - New in Java 8Java - New in Java 9Java - New in Java 10Java - New in Java 11
 
MASTER Java  

Top ranked courses to help you master Java skills.
Java Programming Masterclass

iconicon

Offered By - Tim Buchalka
Platform - Udemy
Rating - * * * * *
Students Enrolled - 575,000 +

RECOMMENDED RESOURCES
Behaviorial Interview
Top resource to prepare for behaviorial and situational interview questions.

STAR Interview Example