Thursday, February 26, 2009

Java Collections

Collections API

The Collections API is a set of classes and interfaces that support operations on collections of objects.
Example of classes: HashSet, HashMap, ArrayList, LinkedList, TreeSet and TreeMap.
Example of interfaces: Collection, Set, List and Map.

DIFFERENCE : Iterator and ListIterator

Iterator : Enables you to cycle through a collection in the forward direction only, for obtaining or removing elements
ListIterator : It extends Iterator, allow bidirectional traversal of list and the modification of elements

DIFFERENCE : HashMap and HashTable

1. 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).
2. HashMap does not guarantee that the order of the map will remain constant over time.
3. HashMap is non synchronized whereas Hashtable is synchronized.
4. Iterator in the HashMap is fail-safe while the enumerator for the Hashtable isn't.

DIFFERENCE : Set and list

A Set stores elements in an unordered way and does not contain duplicate elements, whereas a list stores elements in an ordered way but may contain duplicate elements.

DIFFERENCE : Vector and ArrayList

Vector is synchronized whereas ArrayList is not. The Vector class provides the capability to implement a growable array of objects. ArrayList and Vector class both implement the List interface. Both classes are implemented using dynamically resizable arrays, providing fast random access and fast traversal. In vector the data is retrieved using the elementAt() method while in ArrayList, it is done using the get() method. ArrayList has no default size while vector has a default size of 10. when you want programs to run in multithreading environment then use concept of vector because it is synchronized. But ArrayList is not synchronized so, avoid use of it in a multithreading environment.

DIFFERENCE : Array and Arraylist

An ArrayList is resizable, where as, an array is not. ArrayList is a part of the Collection Framework. We can store any type of objects, and we can deal with only objects. It is growable. Array is collection of similar data items. We can have array of primitives or objects. It is of fixed size. We can have multi dimensional arrays.

DIFFERENCE : 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.

DIFFERENCE : Enumeration, ArrayList, Hashtable and Collections and Collection

Enumeration: It is series of elements. It can be use to enumerate through the elements of a vector, keys or values of a hashtable. You can not remove elements from Enumeration.

ArrayList: It is re-sizable array implementation. Belongs to 'List' group in collection. It permits all elements, including null. It is not thread -safe.

Hashtable: It maps key to value. You can use non-null value for key or value. It is part of group Map in collection.

Collections: It implements Polymorphic algorithms which operate on collections.

Collection: It is the root interface in the collection hierarchy.

HOW : Make hashmap synchronized

Note on Some Important Terms
1) Synchronized means only one thread can modify a hash table at one point of time. Basically, it means that any thread before performing an update on a hashtable will have to acquire a lock on the object while others will wait for lock to be released.

2) Fail-safe is relevant from the 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.

HashMap can be synchronized by

Map m = Collections.synchronizeMap(hashMap);

4 comments:

  1. Please explain in Detail:
    4. Iterator in the HashMap is fail-safe while the enumerator for the Hashtable isn't

    ReplyDelete
  2. Hi vinayak, first the HashMap is not synchronized so in multiple thread environment, if one thread is trying to access and another one is update the map data then in this case first thread will get modified map data and not the previous one.

    ReplyDelete
  3. The iterators for the concrete collection implementations are fail-fast.
    That means that if you are using an Iterator to traverse a collection while underlying collection is being modified by another thread, then the Iterator fails immediately by throwing a ConcurrentModificationException (another
    RuntimeException). That means the next time an Iterator method is called, and the underlying collection has been
    modified, the ConcurrentModificationException exception gets thrown.

    ReplyDelete
  4. fail-safe means, The ability of a system to fail but not produce a catastrophic result.

    ReplyDelete