Friday, February 19, 2010

Containers

1. What do you know about Containers?

There are two types of container library available in java.

1. Collection: Collection category only holds one item in each location. This includes List and Set Interfaces.
a. List (ArrayList, Vector, LinkedList): A collection of elements, duplicates allowed.
b. Set (HashSet, LinkedHashSet, TreeSet): A collection of unique elements.

2. Map: The Map holds key-value pairs, rather like a mini database. We can look up on the basis of key. This includes Map interface.
a. Map (HashMap, HashTable, LinkedHashMap, TreeMap): A mapping of keys to values.

----------------------------------------------------------------------


2. Difference between HashTable and HashMap?


The HashTable is almost equal to HashMap except following points.


1. HashTable is synchronized, while HashMap is not.
2. HashTable doesn't permit null as key while in HashMap it is allowed.
3. HashTable guarantees that order of the table will remain constant over time while in HashMap it is not.

----------------------------------------------------------------------

3. What are the differences between Vector and ArrayList?

The two classes are very similar; however there are few differences, which can give one a clear idea about their usages. Vector is synchronized. Content in vector is thread-safe. ArrayList is not synchronized hence its content is not thread-safe. Synchronization is not at free of cost. It affects vector's performance. So if we don't require thread-safe data, we should use ArrayList.


Internally, both the ArrayList and Vector hold data in Array. When we add a new element into ArrayList or Vector, and the object will need to expand its internal array if it runs out of memory. Vector doubles the size of Array by default, while the ArrayList increases its size by 50 percent. Vector does have slight advantage since they allow to reset increment size. Addition and deletion operation in both classes takes same time.
Addition at the end of container can be performed in constant time O(1). Deletion and Addition are more expansive when the element is added/removed from middle, it takes O(n-i). Here n is number of elements in the container and i is index of the element.

----------------------------------------------------------------------

4. What are the differences between ArrayList and LinkedList?

Both classes implement List interface. Order is the most important feature of a List; it promises to maintain elements in a particular sequence.

Following are the main differences -
1. An ArrayList implemented with Array, allowed fast random access to an element, while LinkedList are optimal for sequential access and are relatively slow for random access.
2. ArrayList is slow when inserting or removing element from middle of the List while LinkedList provide inexpensive insertion and deletion from the middle of the List.

----------------------------------------------------------------------

No comments:

Post a Comment