Dictionaries, HashSets, HashTables, and HashBrowns

Yesterday I was asked a question about what the differences were between a dictionary, a hashset, and a hashtable.  While I was able to reason through part of the question, I wanted to follow up with a blog post describing how each of the data structures relate.

Hashtables and Dictionary is a collection key-value data pairs that allows you to store a key and relate it back to a value.

Hashsets implement the Set interface which maintains that the collection contains only unique elements.

Differences between Hashtables and Dictionaries

The differences between Hashtables and Dictionaries in C#:

Dictionaries are: Generic, Not thread-safe, Output KeyValuePair for enumeration, potentially faster for value types (No unboxing-needed)

Hashtables - Non-generic, thread-safe, Output DictionaryEntry for enumeration, potentially slower for value types. Hashtable can only store object types, therefore when working with value types boxing/unboxing must occur.

Both are: Internally manged by a hash table data structure, need immutable and unique keys.

Differences between a Dictionary and Hashset

While these two data structures are both based on a Hashtable structure, they serve two different implementation purposes. Dictionaries are for when you need to relate a key and a value, while Hashsets are for when you need to keep a unique collection of elements.

Different Types of Dictionaries in C#

I didn't know, but there are a lot of specialized dictionary implementations in the common libraries that can be useful in specific situations.

ConcurrentDictionary (Docs

A thread-safe dictionary that can be accessed by multiple threads concurrently.

HybridDictionary (Docs)

A specialized implementation of a hashtable that will use an underlying list implementation for small count, but after reaching a threshold of the count will convert itself to a hashtable implementation. Not generic!

OrderedDictionary (Docs)

A dictionary that allows you access through key or index value. Not generic!

SortedDictionary (Docs)

A SortedDictionary is backed by a binary tree to support O(log n) searches. It also performs faster "TryGetValue" calls due to its underlying implementation. It is generic.

StringDictionary (Docs)

A dictionary that is strongly typed against strings, and has some performance benefits if that is the only type needed to support. Keys are handled in case-insensitive manner, opting for translating to lowercase.

Comments

Popular posts from this blog

Uncle Bob's Clean Architecture

C4 Model: Describing Software Architecture

Running RabbitMQ with Docker