HashTable
|
ArrayList
|
It is
a map. |
It is
a list. |
You
can add element to ArrayList using add method as shown below:
ArrayList sample = new ArrayList();
sample.add(10);
Here implicit key is the index position in which the element is stored
which is always numeric.
|
You
can add element to HashTable in the form of key-value pair using add
method as shown below:
HashTable sample = new HashTable();
sample.add("key1","stringvalue");
Key can be numeric or alpha.
|
Retrieving
elements is faster since you retrieve an element using its key. |
Retrieval
is comparatively slow since you iterate over the elements in the list
to retrieve a specific element. |
In an
ArrayList, you can store elements of the same type only. If you store
elements of different types in the same ArrayList then you will get
run time error while retrieving it. You will retrieve and display
elements of ArrayList as shown below:
foreach(<datatype> element in <ArrayList>) {
Console.WriteLine(element)
}
|
In the
same HashTable, you can store elements of different data types and
retrieve them without any errors using DictionaryEntry as shown below:
foreach(Dictionaryentry dirSample in <HashTable>.keys.values){
Console.Writeline(dirSample.keys
+ "," + dirSample.values);
}
|