ArrayList Vs HashTable

ArrayList and HashTables are data structures provided by .NET framework.Both are collections and are bundled under namespace System.Collections.

To create an ArrayList :
ArrayList employees = new ArrayList();
employees .Add("Ram");


To create a HashTable:HashTable employees = new HashTable();
employees .Add("E1001,"Ram");


To understand, the difference we need to understand the way they store objects.In case of an ArrayList, it keeps on incrementing the index and stores the objects as you add them.The elements stored will not be sorted automatically, unless you explicitly sort them, by using Sort() or ReverseSort() methods of arraylist.In case of a HashTable, hash value of the key is computed and the object is stored in respective bucket.So the elements are stored in a sorted order. This provides an advantage when it comes to searching a specific element. Incase of a hash table, it is faster as compared to the array list.Search is simplified by searching in only one particular bucket, thus substantially reducing the number of key comparisons required to find an element.

Another comparison is that the index elements in an ArrayList have a numeric index, whereas you index elements in a HashTable with a key which can be a string .

Comments

Popular posts from this blog

Technical Interview Screen

Natural Face Pack

Cloud Drives