MY mENU


Thursday 23 February 2012

Enumeration Vs Iterator in Collections

What is the difference between Enumeration And Iterator?



Enumeration

Iterator
1. It is legacy interface and introduced in 1.0 version

1 It is non-legacy and introduced in 1.2 version
2.Applicable only for legacy classes and it is not universal cursor

2. Applicable for any Collection implemented class object.
3.While iterating the elements we are not allowed to remove the objects just we can perform only read operation

3. While iterating we can perform removal also in addition to read operation.
4.By using elements() method we can get Enumeration object

4.   By using iterator() method we can get Iterator   
    object


Iterator
Enumerator

Iterator supports remove() method


Enumerator doesn’t supports remove() method.

It is nor synchronized


It is synchronized


It supports ArrayList, vector, hashmap, hashtable


It doesn’t supports Arraylist, and hashmap.

It doesn’t supports legacy methods


It supports legacy methods like hasMoreElements(),nextElements();

ArrayList Vs LinkedList


ArrayList

LinkedList
1. The underlying data structure is resizable or growable array.

1. The underlying data structure is Double Linked List.
2.  This is Best choice if frequent operation is retrieval and worst choice if frequent operation is insertion or deletion in the middle.

2.  This is Best choice  if frequent operation is insertion or deletion in the middle and worst choice if frequent operation is retrieval .
3. This class implements Serializable , Cloneable and RandomAccess interfaces.

3. This class implements Serializable , Cloneable but not  RandomAccess interface.

Set Interface IN Collections


Set Interface:

Set is a child interface of Collection interface. it can be used to represent a group of individual objects as a single entity where
  • Duplicate objects are not allowed.
  • Insertion order is not preserved
Shorted Set:

it is child interface of Set interface. it can be used to represent a group of individual objects in to a single entity where
  • All the objects are arranged in some sorting order (Can be natural sorting order or customizede).
  • Duplicates are not allowed.
NavigableSet:

It is child interface of SortedSet and provides several utility methods for navigation purposes
  • It doesn’t allows duplicates                                
  • Insertion order is preserved
  • It is introduced in 1.6 version

Arrays Vs Collections



                              Arrays

Collections
1.  Arrays r fixed in size and hence once we created an array we are not allowed to increase or decrease the size based on our requirement.

1. Collections are growable in nature and hence based on our requirement we can increase or decrease the size.
2.  Memory point of view arrays are not recommended to use

2. Memory point of view collections are recommended to use.
3. Performance point of view arrays are recommended to use

3. Performance point of view collections are not recommended to use.
4.  Arrays can hold only homogeneous elements

4. Collections can hold both homogeneous and heterogeneous elements.
5. Arrays can hold both primitives as well as objects

5. Collections can hold only objects.
6. For any requirement, there is no ready method support compulsory programmer has to code explicitly.6. For every requirement ready made method support is available. Being a programmer we have to know how to use those methods and we are not responsible to implement those.