Wednesday, September 24, 2008

Interview Q & A: Java Collections Framework

#1 What is the difference between arrays and Collections in java?

* Arrays cannot be resized whereas Collections can be resized and are form of dynamic arrays.
* Arrays forces to specify the initial size whereas Collection does not.
* Arrays can store primitives whereas Collection stores only objects.

#2 What is Collection framework?

Collection framework provides a set of classes and interfaces for sorting and manipulating group of objects as a single unit.

#3 What are similarities and differences between Iterator and Enumeration interface?

Similarities:
* Iterator and Enumeration are used to step through elements of a Collection.

Differences:
* Enumeration has methods only to traverse and fetch objects serving as read-only whereas Iterator has methods to manipulate objects like adding/removing objects. So use Enumeration whenever we need to have Collection objects as read-only.

#4 What is the difference between java.util.List and java.util.Set interfaces?

* List interface allows duplicate elements whereas Set does not allow duplicate elements.
* List is an ordered collection and also allows positional indexing (add or retrieving elements at a particular index) whereas Set is an unordered collection.

#5 What are SortedSet/SortedMap used for?

Interface for maintaining elements in sorted order and implementation classes are TreeSet/TreeMap.

#6 What is the difference between java.util.Queue and java.util.Stack interfaces?

Queue supports ordering of element in FIFO basis. That is element first added is the first element to get Whereas Stack supports ordering element in LIFO basis.

#7 What is the difference between ArrayList and LinkedList?

* ArrayList can be used wherever we need to support random access of elements without adding or removing elements of the list.
* LinkedList can be used wherever we need to add, retrieve and remove elements at the beginning and end of the list.

#8 What is the difference between HashMap and TreeMap?

* TreeMap maintains the order and HashMap does not.
* HashMap is faster for adding, retrieving, removing of elements from the collection and TreeMap is faster during traversing in a sorted manner.

#9 What is the difference between Vector and ArrayList?

* Vector is synchronized whereas ArrayList is not. Arraylist as is unsynchronized gives better performance than Vector in a non-multithreaded environment and Vector gives better performance in multithreaded environment.
* Vector has a default size of 10 whereas ArrayList has no default size.

#10 What is the difference between HashTable and HashMap?

* HashTable is synchronized whereas HashMap is unsynchronized. HashMap gives better performance in non-threaded environment as is unsynchronized when compared with HashTable.
* HashTable does not allow null whereas HashMap allows null as key/value.

#11 How to synchronize a HashMap?

Using Collections.synchronizeMap(hashMap)

No comments: