leonidas blog

Grip. Stand. Throw.

This Blog





Email Notifications


Hashtable, ListDictionary and HybridDictionary

In the most basic case, the Hashtable class is used to do mapping of key/value pairs of objects.

For  example, assume that you have User ID mapped to User's Fullname:

Hashtable users = new Hashtable();

users["001"] = "Marasigan, Raimund";
users["002"] = "Marasigan, Myrene";
users["003"] = "Alcaras, Mong";
users["004"] = "Castro, Diego";
users["005"] = "Dizon, Mike";

Hashtable is specifically designed to handle large amount of collections. Unfortunately, there are some inefficiencies in Hashtable that can impede performance for small collections (10 items or less). That is where the ListDictionary comes in. It is implemented as a simple array of items underneath the hood, so it is very efficient for small collections. However, if you use ListDictionary for larger lists it is not efficient at all. So if you know your collection is small, use ListDictionary; if your collection is large, use Hashtable.

Okay. But what if you do not know how large your collection? The solution for this is to use HybridDictionary. Surprisingly, by default, it is implemented as a ListDictionary and when the collection becomes too large it automatically convert itself into a Hashtable.

The HybridDictionary is best used in situation where some collections are small and others are very large.


Published 08-13-2007 1:15 AM by leonidas

Filed under: , ,


# re: Hashtable, ListDictionary and HybridDictionary@ Saturday, August 18, 2007 2:32 PM

Yup! and HybridDictionary is threadsafe too ;)