Earlier I have shared 100+ tutorials on Java collections framework. In this article I am going to share interview questions on Java collections framework. If you are new to collections, I would recommend you to refer these tutorials before going through the below set of questions as they would help you to learn the basics of collections. Let’s start…
Almost in every interview you would be asked to explain the difference between two collections classes or two collections interfaces, the “difference between” questions have become so popular among interviewers that they comprises around 90% of the questions asked during interviews. I have almost covered all the frequently asked differences in collections. Here are they:
Q) What is the difference between ArrayList and Vector?
A) Refer this article: Arraylist vs. Vector
Q) What is the difference between ArrayList and LinkedList?
A) Refer: ArrayList vs. LinkedList
Q) What is the difference between HashMap and Hashtable?
A) Refer: HashMap vs. Hashtable
Q) What is the difference between HashSet and TreeSet?
A) Refer: HashSet vs. TreeSet
Q) What is the difference between ArrayList and HashMap?
A) Refer: ArrayList vs. HashMap
Q) What is the difference among List, Set and Map interfaces?
A) Refer: List vs. Set vs. Map
Q) What is the difference between Iterator and ListIterator?
A) Refer: Iterator vs. ListIterator
Q) What is the difference between Iterator and Enumeraton?
A) Here are the few differences between them:
1) Iteator can be used on legacy(Vector etc.) classes as well as non-legacy classes (ArrayList, LinkedList etc.), whereas Enumeration is basically used for legacy classes such as Vector.
2) Iterator is comparatively slower than Enumeration.
3) Iterator is fail-fast wheras Enumeration is not.
Q) How to synchronize HashMap?
A) Refer: HashMap synchronization.
Q) How to synchronize ArrayList?
A) Refer: ArrayList synchronization.
Q) What is the difference between fail-fast and fail-safe?
A) fail-fast Iterators throw ConcurrentModificationException when there is a structural modification in underlying collection whereas fail-safe doesn’t throw any exception when they detect structural modification as fail-safe iterators works with clones of underlying collection.
Q) How to convert array to ArrayList?
A) Refer: Array to ArrayList conversion.
Q) How to remove repeated elements from ArrayList?
A) Refer this tutorial.
Q) How to loop ArrayList?
A) Refer this tutorial.