Tuesday, July 19, 2011

Collection Framework Interview Questions

Interview questions in Java on Collection framework

What is Java Collections API ?
Java Collections framework API is a unified architecture for representing and manipulating collections. The API contains Interfaces, Implementations & Algorithm to
help java programmer in everyday programming. This API does 6 things at high level
Reduces programming efforts. - Increases program speed and quality.
Allows interoperability among unrelated APIs.
Reduces effort to learn and to use new APIs.
Reduces effort to design new APIs.
Encourages & Fosters software reuse.
To be specific, There are six collection java interfaces. The most basic interface is Collection. Three interfaces extend Collection: Set, List, and SortedSet. The
other two collection interfaces, Map and SortedMap, do not extend Collection, as they represent mappings rather than true collections.

What is an Iterator?
Some of the collection classes provide traversal of their contents via a java.util.Iterator interface. This interface allows you to walk through a collection of
objects, operating on each object in turn. Remember when using Iterators that they contain a snapshot of the collection at the time the Iterator was obtained;
generally it is not advisable to modify the collection itself while traversing an Iterator.

What is the difference between java.util.Iterator and java.util.ListIterator?
Iterator : Enables you to traverse through a collection in the forward direction only,
ListIterator : extends Iterator, and allows bidirectional traversal of list and also allows the modification of elements.

What is HashMap and Map?
Map is Interface which is part of Java collections framework. This is to store Key Value pair, and Hashmap is class that implements that using hashing technique.

Difference between HashMap and HashTable?
Both Hashtable & HashMap provide key-value access to data.
The Hashtable is one of the original collection classes in Java (also called as legacy classes). HashMap is part of the new Collections Framework, added with
Java 2, v1.2.
The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.(HashMap allows null values as key and value whereas Hashtable doesn’t allow nulls).
HashMap does not guarantee that the order of the map will remain constant over time. But one of HashMap's subclasses is LinkedHashMap,
so in the event that you'd want predictable iteration order (which is insertion order by default), you can easily swap out the HashMap for a LinkedHashMap.
This wouldn't be as easy if you were using Hashtable.
HashMap is non synchronized whereas Hashtable is synchronized.
Iterator in the HashMap is fail-fast while the enumerator for the Hashtable isn't. So this could be a design consideration.

What is fail-fast property?
At high level - Fail-fast is a property of a system or software with respect to its response to failures. A fail-fast system is designed to immediately report any
failure or condition that is likely to lead to failure.When a problem occurs, a fail-fast system fails immediately and visibly. Failing fast is a non-intuitive technique:
"failing immediately and visibly" sounds like it would make your software more fragile, but it actually makes it more robust. Bugs are easier to find and fix, so
fewer go into production.
In Java, Fail-fast term can be related to context of iterators. If an iterator has been created on a collection object and some other thread tries to modify the
collection object "structurally", a concurrent modification exception will be thrown. It is possible for other threads though to invoke "set" method since it
doesn't modify the collection "structurally". However, if prior to calling "set", the collection has been modified structurally, "IllegalArgumentException" will
be thrown.

We can make Hashmap synchronized?
HashMap can be synchronized by Map m = Collections.synchronizedMap(hashMap);

Where will you use Hashtable and where will you use HashMap?
1. The basic difference between a Hashtable and an HashMap is that, Hashtable is synchronized while HashMap is not. Thus whenever there is a possibility of multiple
threads accessing the same instance, one should use Hashtable. While if not multiple threads are going to access the same instance then use HashMap.
Non synchronized data structure will give better performance than the synchronized one.
2. If there is a possibility in future that - there can be a scenario when you may require to retain the order of objects in the Collection with key-value pair
then HashMap can be a good choice. As one of HashMap's subclasses is LinkedHashMap, so in the event that you'd want predictable iteration order
(which is insertion order by default), you can easily swap out the HashMap for a LinkedHashMap.This wouldn't be as easy if you were using Hashtable.
Also if you have multiple thread accessing you HashMap then Collections.synchronizedMap() method can be leveraged. Overall HashMap gives you more flexibility in
terms of possible future changes.

Difference between Vector and ArrayList?
Vector & ArrayList both classes are implemented using dynamically resizable arrays, providing fast random access and fast traversal.
ArrayList and Vector class both implement the List interface. Both the classes are member of Java collection framework, therefore from an API perspective,
these two classes are very similar. However, there are still some major differences between the two. Below are some key differences
Vector is a legacy class which has been retrofitted to implement the List interface since Java 2 platform v1.2
Vector is synchronized whereas ArrayList is not. Even though Vector class is synchronized, still when you want programs to run in multithreading environment
using ArrayList with Collections.synchronizedList() is recommended over Vector.
ArrayList has no default size while vector has a default size of 10.
The Enumerations returned by Vector's elements method are not fail-fast. Whereas ArraayList does not have any method returning Enumerations.

What is the Difference between Enumeration and Iterator interface?
Enumeration and Iterator are the interface available in java.util package. The functionality of Enumeration interface is duplicated by the Iterator interface.
New implementations should consider using Iterator in preference to Enumeration. Iterators differ from enumerations in following ways:
Enumeration contains 2 methods namely hasMoreElements() & nextElement() whereas Iterator contains three methods namely hasNext(), next(),remove().
Iterator adds an optional remove operation, and has shorter method names. Using remove() we can delete the objects but Enumeration interface does not support this
Enumeration interface is used by legacy classes. Vector.elements() & Hashtable.elements() method returns Enumeration. Iterator is returned by all Java Collections
Framework classes. java.util.Collection.iterator() method returns an instance of Iterator.

What is an enumeration?
An enumeration is an interface containing methods for accessing the underlying data structure from which the enumeration is obtained. It is a construct which
collection classes return when you request a collection of all the objects stored in the collection. It allows sequential access to all the elements stored in
the collection.

What is the difference between Enumeration and Iterator?
The functionality of Enumeration interface is duplicated by the Iterator interface. Iterator has a remove() method while Enumeration doesn't.
Enumeration acts as Read-only interface, because it has the methods only to traverse and fetch the objects, where as using Iterator we can manipulate the
objects also like adding and removing the objects. So Enumeration is used when ever we want to make Collection objects as Read-only.

Where will you use Vector and where will you use ArrayList?
The basic difference between a Vector and an ArrayList is that, vector is synchronized while ArrayList is not. Thus whenever there is a possibility of multiple
threads accessing the same instance, one should use Vector. While if not multiple threads are going to access the same instance then use ArrayList.
Non synchronized data structure will give better performance than the synchronized one.

What is the difference between ArrayList and LinkedList?
(ArrayList vs LinkedList.) java.util.ArrayList and java.util.LinkedList are two Collections classes used for storing lists of object references
some key differences:
ArrayList uses primitive object array for storing objects whereas LinkedList is made up of a chain of nodes. Each node stores an element and the pointer to the
next node. A singly linked list only has pointers to next. A doubly linked list has a pointer to the next and the previous element. This makes walking the
list backward easier.
ArrayList implements the RandomAccess interface, and LinkedList does not. The commonly used ArrayList implementation uses primitive Object array for internal
storage. Therefore an ArrayList is much faster than a LinkedList for random access, that is, when accessing arbitrary list elements using the get method. Note
that the get method is implemented for LinkedLists, but it requires a sequential scan from the front or back of the list. This scan is very slow. For a LinkedList,
there's no fast way to access the Nth element of the list.
Adding and deleting at the start and middle of the ArrayList is slow, because all the later elements have to be copied forward or backward.
(Using System.arrayCopy()) Whereas Linked lists are faster for inserts and deletes anywhere in the list, since all you do is update a few next and previous
pointers of a node.
Each element of a linked list (especially a doubly linked list) uses a bit more memory than its equivalent in array list, due to the need for next and previous
ArrayList may also have a performance issue when the internal array fills up. The arrayList has to create a new array and copy all the elements there.
The ArrayList has a growth algorithm of (n*3)/2+1, meaning that each time the buffer is too small it will create a new one of size (n*3)/2+1 where n is the
number of elements of the current buffer. Hence if we can guess the number of elements that we are going to have, then it makes sense to create a arraylist with
that capacity during object creation (using construtor new ArrayList(capacity)). Whereas LinkedLists should not have such capacity issues.

Where will you use ArrayList and Where will you use LinkedList?
The Java SDK contains 2 implementations of the List interface - ArrayList and LinkedList. If you frequently add elements to the beginning of the List or
iterate over the List to delete elements from its interior, you should consider using LinkedList. These operations require constant-time in a LinkedList
and linear-time in an ArrayList. But you pay a big price in performance. Positional access requires linear-time in a LinkedList and constant-time in an ArrayList.

1 comment: