Thursday, August 23, 2012

Collection API Questions - 1

Java Collection Framework is one of the hot favourite among interviewers. The reason for that is any Java program uses Java Collection Framework intensively and any manager hiring for his team would like to see that you have basic and some advanced knowledge in the Collection API. Other reason that I find for interest in Collection Framework is that the questions will help in identifying whether the candidate knows how the various Data Structures are implemented in Java explaining his interest. 

In this post I will try to list some of the beginner to advanced level questions asked on the Collection Framework.


Question 1: What is the Java's collection API?
Java's Collection API is a unified architecture of interfaces, classes and algorithms that are required for representing and manipulation of Collections. For example, List, Map, Set and the like. Benefit of these classes is as follows:
1.        It reduces the programming effort for the developers.
2.        It increases the program speed and quality, since the Collection API provides the high-performance and high quality implementation of the data structure and the algorithms required for their manipulation.
2.        It increase software re-usability.
3.        Reduces efforts to learn and create basic data structures API’s

An excerpt from the Java's Collection Trail 



















Question 2: What are the basic interfaces in the Collection API?
Basic interfaces in the collection API are:

1. Collection: This is the parent interface and all other interfaces extend this interface.
2. Set: This is a core interface to represent a mathematical set, the basic thing about a set is it 
             does not have duplicate values and at most have one null value.
3. List: An ordered collection or sequence, the user of this interface has precise control over 
            where each element is inserted. The user can access each element by its index /  
            position. It differs from the set in the way, that it allows duplicates and null values.
4. Queue: This is a collection used to hold multiple elements prior to processing. Queues 
                  typically, but do not necessarily, order elements in a FIFO (first-in, first-out) manner. 
                 Among the exceptions are priority queues, which order elements according to a  
                 supplied comparator or the elements' natural ordering. Whatever the ordering used, 
                 the head of the queue is the element that would be removed by a call to remove or  
                 poll. In a FIFO queue, all new elements are inserted at the tail of the queue. Other 
                 kinds of queues may use different placement rules.
5. Map: This is a collection of key-value pair. A Map cannot contain duplicate keys; each key 
              can map to at most one value.

The other two core interfaces are merely sorted versions of set and map.

1. SortedSet: This is a Set that maintains its elements in ascending order. Several additional  
                        operations are provided to take advantage of the ordering.
2. SortedMap: This is a Map that maintains its mappings in ascending key order.

Their hierarchy is depicted in the below diagram.


Question 3: What is the difference between Set and List?
The basic difference between a Set and List is that set does not allow duplicates and allows at most one null value. Also, List is an ordered collection of elements whereas set is an unordered collection of elements.

Question 4: Why doesn't Map interface extend Collection?
Theoretically, let us assume that Map extends Collection, So what exactly are the elements of this collection? The only answer to this is "key-value" pair. But you cannot really iterate over these elements without knowing the value of key. However, Map can be viewed as a collection of the keys, values, key-value pair or entrySets. Depicting this behaviour Map does provide the collection view method keySets(), values(), entrySets().

Similary, if the Collection interface extends Map interface, then what are the keys of these elements. There is no satisfactory example.While it is possible to view List as a possible map with the indices as the keys, this has a property that when you remove an element from the List, it changes the keys of the elements preceding it and hence the reason that we don't have Map view operations on the list.

Question 5: Why doesn't Collection extends Cloneable or Serializable interface?
The only answer I can think of is that it is rarely required to clone or serialize the whole collection. There are serializable / cloneable implementations of the Collection. So, if the user want to create a Cloneable/Serializable collection, he can create a generic collection and then can use addAll() to insert the collection in a cloneable / serializable collection implementation.

Question 6: What are the other useful interfaces in Collection Framework?
Other useful interfaces are:

  • Iteration : Enumerator, Iterator, ListIterator
  • Ordering: Comparable, Comparator
  • Performance: RandomAccess
Question 7: What is the difference between Enumerator, Iterator and ListIterator?
Property
Enumeration
Iterator
ListIterator
Number of method
2
3
9
Method names
hasMoreElements(), nextElement()
hasNext(), next(), remove().
add(), hasNext(), next(), hasPrevious(), previous(), nextIndex(), previousIndex(), set(),remove().
Access Direction
Forward-only
Forward only
Bi-directional
Summary
It provides a read only traversal of the collection. Legacy classes such as Vector and HashTable returns this
Iterator provides a method to remove an element from the iterator. Java Collection Framework classes return this.
It provides a bi-directional access on the collection and hence has methods to support it, like previous() and next(). It also allows the modification of elements using set() and add() operation.