Thursday, September 6, 2012
Java Collection interview Questions Answers | Java Collection FAQ
Java Collection Framework interview questions and answers contains 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 is one of the most asked concept 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 there 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 popular area from where Java collection interview Questions asked during 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.
Here is list of my favorite, frequently asked Questions from Java collection framework. Almost all of these questions have appeared in Java interview at various level ranging from Junior to Senior software engineer level at different Companies e.g. Capegemini, Tech Mahindra, TCS , Satyam and CTS.
What is Difference between Hashtable and HashMap in Java?
This Java collection interview questions 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 are many difference between them but most important is thread-safety, HashMap is not thread-safe while Hashtable is thread-safe collection. See Hashtable vs HashMap in Java for more differences between HashMap and Hashtable in Java.
What is 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 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 ability to remove object while traversing while Enumeration doesn't allow you to remove 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 concept of fail-safe iterator is come along with ConcurrentHashMap and CopyOnWriteArrayList. See Difference between fail-safe and fail-fast Iterator in Java for 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 appear 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 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 difference between Vector and ArrayList in Java for complete answer of this collection interview question in Java.
What is 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 there 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 difference between List and Set in Java ?
List vs Set is one of the most important concept 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
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 answer of this Java collection question.
These were some of the frequently asked Java collection framework interview question 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. If you have any interesting Java collection interview question or are you looking answer for any Java collection question then please post here.
Other Interview questions and Answers post