Hello Folks, collection is the favorite and popular among interviewers. Here is the list of most frequently asked collection interview questions and their answers.
Click here if you want to learn more about collection framework and their praticle implementations.
1). What is a collection framework ?
A collection framework is a class library to handle group of objects. Collection framework is implemented in java.util.package which gives you lists, sets, maps and queue to satisfy most of your coding needs.
There are few basic operations we normally use with collections –
- Add an object to the collection.
- Remove object from the collection.
- Find out if an object (or group of objects) is in the collection.
- Retrieve an object from the collection (without removing it).
- Iterate through the collection, looking at each element (object) one after another.
Interface and class hierarchy for collections –
2). Define Sets, Lists, Queues and Maps ?
Sets – A Sets represent a group of elements arranged just like an array. The set will grow dynamically when the elements are stored in into it. A set will not allow duplicate elements. If we try to pass the same element that is already available in the set, then it is not stored into to the set.
Lists – Lists are like sets. They store a group of elements. But lists allow duplicate values to be stored.
Queues – A queue represents arrangement of elements in FIFO (first in First out) order. This means that an element that is stored as a first element into the queue will be removed first from the queue.
Maps – Maps stores element in the form of key and value pairs. If the key is provided then its corresponding value can be obtained. Of course, the keys should have unique values.
3). Can you store a primitive data type into a collection ?
No, Collections store only objects.
4). What are the difference between Sets and Lists ?
|Set is unordered collection.||List is ordered collection based on zero based index.|
|Set doesn’t not allow duplicate elements.||List allow duplicate elements.|
|Set will allow only one null element.||List doesn’t prevent inserting null elements (as many you like).|
5). What are the difference between ArrayList and Vector ?
|ArrayList object is not synchronized by default.||Vector object is synchronized by default.|
|In case of single thread, using ArrayList is faster than the Vector.||In case of multiple thread, using Vector is advisable. With a single thread, Vector become slow.|
|ArrayList increases its size every time to 50 percent.||Vector increases its size every time by doubling it.|
|ArrayList can use only Iterator to access the elements.||Vector list can use Iterator and Enumeration|
6). Can you synchronize the ArrayList object ?
Yes, we can use synchronizedList() method to synchronize the ArrayList.
7). What is the difference between Enumeration and Iterator ?
|Enumeration doesn’t have a remove() method.||Iterator has a remove() method.|
|Enumeration act as Read-only interface, because it has the method only to traverse and fetch the objects.||Can be abstract, final, native, static or synchronized.|
8). What are the difference between HashMap and HashTable ?
|HashMap is not synchronized.||HashTable is synchronized.|
|HashMap allows one null key and any number of null values.||HashTable does not allow null key or values.|
|Iterator in HashMap is fail-fast.||Enumerator in Hashtable is not fail-fast.|
|Hashmap is much faster and uses less memory than Hashtable as former is unsynchronized. Unsynchronized objects are often much better in performance in compare to synchronized object.||HashTable is less faster than HashMap as its methods are synchronized.|
|We can make the HashMap as synchronized by calling this code
Map m = Collections.synchronizedMap(hashMap);
|Hashtable is internally synchronized and can’t be unsynchronized.|
Click here for more information.
9). What are the difference between fail-fast and fail-safe iterators ?
|Fail-fast Iterator||Fail-safe Iterator|
|Fail as soon as they realize that structure of Collection has been changed since Iteration has begun and throw ConcurrentModificationException.||Doesn’t throw any Exception if Collection is modified structurally.|
|This is not a guranteed behaviour instead it’s done of “best effor basis”.||This work on clone of Collection instead if original collection.|
|Iterator returned by most collection are fail-fast including Vector, ArrayList, HashSet etc.||Iterator of CopyOnWriteArrayList, ConcurrentHashMap leyset is an exmaple.|
10). What are the difference between HashSet and TreeSet ?
|HashSet is under set interface. i.e. it does not gurantee for either sorted order or sequence order.||TreeSet is uinder set i.e. it provides elements in an sorted order.|
|We can any type of element to HashSet.||We can add only similar type of elements to tree set.|
Please leave your comment if you have any questions/suggestions/concern.