Top 10 Java Collection interview Questions Answers | Java Collection FAQ

Java Collection Framework interview questions and answers contain questions from popular Java collection classes e.g. HashMap, ArrayList, HashSet, ConcurrentHashMap and legacy collection classes like Vector and Hashtable. Interview questions from Java Collection framework are one of the most asked concepts on any Core Java interview or J2EE interview at both beginners, experienced and intermediate level e.g. 2 to 4 years experienced Java programmer. In order to answer interview questions from Java collection framework, You need a good knowledge of various Collection interfaces e.g. Map, List and Set and familiarities with their frequently used implementation e.g. HashMap, Hashtable, TreeMap from Map, ArrayList, LinkedList and Vector for List interface and HashSet, TreeSet of Set interface.

Questions related to Iterating collection, modifying Collection and thread-safety of Collection is also the popular area from where Java collection interview Questions asked during the interview.

In this List of Java collection interview questions and answers, we will see questions from all above area. I have also included some questions from new Concurrent collection classes introduced in Java 5 e.g. ConcurrentHashMap and CopyOnWriteArrayList.



Java Collection Interview Questions Answers

Java Collection Interview Questions Answers | Java Collection FAQHere is a list of my favorite, frequently asked Questions from Java collection framework. Almost all of these questions have appeared in Java interview at the various level ranging from Junior to Senior Software Engineer level at different Companies e.g. Capgemini, Tech Mahindra, TCS, Satyam, and CTS.



What is Difference between Hashtable and HashMap in Java?
This Java collection interview question is I guess most popular one. Most of Java programmer who has at least 2 years of experience has seen this question on core Java or J2EE interview. Well, there is much difference between them but most important is thread-safety, HashMap is not thread-safe while Hashtable is a thread-safe collection. See Hashtable vs HashMap in Java for more differences between HashMap and Hashtable in Java.


What is the difference between Hashtable and ConcurrentHashMap in Java?
Another frequently asked Java collection interview question post-Java 5 world which introduced Concurrent Collection classes like ConcurrentHashMap and CopyOnWriteArrayList along with Concurrency utilities e.g. CyclicBarrier and CountDownLatch. Well, both Hashtable and ConcurrentHashMap are thread-safe here but later provides more scalability than former. See Difference between ConcurrentHashMap and Hashtable in Java for the answer of this Java collection interview question.


What is Difference between Iterator and Enumeration in Java?
One of the classic interview Questions asked on Java collection framework, This is pretty old and programmer who has been working in Java for 4 to 6 years must have seen this question before. Well, Iterator and ListIterator in Java is a new way to iterator collection in Java and provides the ability to remove an object while traversing while Enumeration doesn't allow you to remove the object. See Iterator vs Enumeration in Java for more differences between both of them.


What is Difference between fail-safe and fail-fast Iterator in Java?
This is relatively new Java collection interview question because the concept of a fail-safe iterator is come along with ConcurrentHashMap and CopyOnWriteArrayList. See Difference between fail-safe and fail-fast Iterator in Java for the answer of this Java collection question.



How HashMap works internally in Java?
One of the most frequently asked Java interview question to experience Java programmer of 4 to 5 years of experience. I have seen this question on big companies like Morgan Stanley, JP Morgan, Nomura and other banks e.g. Barclays Capital. See How HashMap works internally in Java for detailed answer of this Java collection interview question.


Can you write code to traverse Map in Java on 4 ways?
Another Java collection question which appears as part of Java Coding interview question and appeared in many interviews. As you know there are multiple ways to traverse or iterate Map in Java e.g. for loop, while loop using Iterator etc. 4 ways to iterator Map in Java has detailed explanation and sample code which is sufficient to answer this Java collection framework interview question.


What is the difference between Vector and ArrayList in Java?
Along with Difference between HashMap and Hashtable, this Java collection interview question is probably second in the list of frequently asked question on Java collection framework. Both ArrayList and Vector implements List interface from Java 4 but they have differences including synchronization, See the difference between Vector and ArrayList in Java for the complete answer of this collection interview question in Java.


What is the difference between ArrayList and LinkedList in Java?
A follow-up question which is asked in response to previous Java collection interview question. Here also both LinkedList and ArrayList are List implementation but their internal data-structure is different, one is derived from Array while other is derived from LinkedList. See LinkedList vs ArrayList in Java to answer this Java Collection interview question.



What is the difference between List and Set in Java?
List vs Set is one of the most important concepts to understand in Java Collection framework and this Java collection interview question focus on that. Most important difference between them is that List allows duplicates and maintain insertion order while Set doesn't allow duplicates and doesn't maintain any order. See Difference between Set and List in Java to see more differences between them.

Here is also a nice summary of different collection classes in Java:

Java Collection interview Questions Answers



How do you find if ArrayList contains duplicates or not?
Since List allows duplicates this becomes a followup question of earlier Java collection framework interview question. See How to check if ArrayList contains duplicates or not for the answer of this Java collection question.

That's all about some of the frequently asked Java Collection interview questions. Btw, If you have any interesting Java collection interview question or are you looking an answer for any Java collection question then please post here. If you are looking for some assistance, you can also check out the Java Programming Interview Exposed book by Noel Markham, an ideal guide to prepare for Java developer interviews. 

Java Collection interview Questions Answers | Java Collection FAQ

These were some of the frequently asked Java collection framework interview questions you can also call them Java Collection FAQ. Collection and Threads are most important part of Java programming language and considered as fundamentals of Java, So always prepare them well before appearing for any Java or J2EE interview

Further Learning
Java Fundamentals: Collections
From Collections to Streams in Java 8 Using Lambda Expressions
Streams, Collectors, and Optionals for Data Processing in Java 8

Other Interview Questions and Answers post
Java Programming Interview Exposed
133 Core Java Interview Questions from last 5 years

Thanks for reading this article so far. If you like these interview questions then please share with your friends and colleagues. If you have any question or feedback then please drop a comment.

2 comments:

  1. From last couple of years Java interviews has changed drastically, now days no one ask about classical questions like difference between Iterator and Enumerator or difference between ArrayList or Vector. In 2014, the most popular question was about usage of different collection interface e.g. Map, Set, List and Queue. Interviewer was also very keen to know how much you know about certain implementation e.g. how ConcurrentHashMap perform better than Hashtable or Synchronized HashMap. How HashSet works and How to implement PriorityQueue by your own.

    In order to prepare on Collections framework I just following items :
    1) Understand interfaces e.g. Map, Set, List, Queue and otherws to figure out key difference e.g. Set doesn't allow duplicate but List does.

    2) Understand different implementation classes e.g. HashMap, Hashtable, ConcurrentHashMap, ArrayList, LinkedList, Vector etc and perform comparative analyssis to find out which Collection class suits which condition.

    ReplyDelete
  2. For Junior Java Developers of 2 to 3 years experience, I would say to prepare well for DS and Algo, you can check some sample Data structure questions here, once you are good at that, just prepare some basic Java questions for telephonic round. Once you are done that you are ready, but if you want more confidence, you should check this Mega list of Java questions, which contains core Java questions including multi-threading, exception handling, collections, GC, design pattern and OOP questions from last 5 years of Java interviews.

    ReplyDelete