Why Is Dictionary Preferred Over Hashtable in C#

Why is Dictionary preferred over Hashtable in C#?

For what it's worth, a Dictionary is (conceptually) a hash table.

If you meant "why do we use the Dictionary<TKey, TValue> class instead of the Hashtable class?", then it's an easy answer: Dictionary<TKey, TValue> is a generic type, Hashtable is not. That means you get type safety with Dictionary<TKey, TValue>, because you can't insert any random object into it, and you don't have to cast the values you take out.

Interestingly, the Dictionary<TKey, TValue> implementation in the .NET Framework is based on the Hashtable, as you can tell from this comment in its source code:

The generic Dictionary was copied from Hashtable's source

Source

.NET HashTable Vs Dictionary - Can the Dictionary be as fast?

System.Collections.Generic.Dictionary<TKey, TValue> and System.Collections.Hashtable classes both maintain a hash table data structure internally. None of them guarantee preserving the order of items.

Leaving boxing/unboxing issues aside, most of the time, they should have very similar performance.

The primary structural difference between them is that Dictionary relies on chaining (maintaining a list of items for each hash table bucket) to resolve collisions whereas Hashtable uses rehashing for collision resolution (when a collision occurs, tries another hash function to map the key to a bucket).

There is little benefit to use Hashtable class if you are targeting for .NET Framework 2.0+. It's effectively rendered obsolete by Dictionary<TKey, TValue>.

Hashtable vs Dictionary : Faster?


However, isn't hashtable's sorted, which could mean that the search could be faster?

I don't believe that hashtable is sorted.

They share a similar underlying implementation, but Dictionary<TKey, TValue> has been recommended over Hashtable for a long time, which will perform better for value types as it eliminates boxing/unboxing.

See https://referencesource.microsoft.com/#mscorlib/system/collections/hashtable.cs,77

If you really want to know, try benchmarking it. BenchmarkDotNet is a great library for that.

Difference between Dictionary and Hashtable

Simply, Dictionary<TKey,TValue> is a generic type, allowing:

  • static typing (and compile-time verification)
  • use without boxing

If you are .NET 2.0 or above, you should prefer Dictionary<TKey,TValue> (and the other generic collections)

A subtle but important difference is that Hashtable supports multiple reader threads with a single writer thread, while Dictionary offers no thread safety. If you need thread safety with a generic dictionary, you must implement your own synchronization or (in .NET 4.0) use ConcurrentDictionary<TKey, TValue>.

List vs Dictionary (Hashtable)

"Faster" depends on what you need them for.

A .NET List is just a slab of continuous memory (this in not a linked list), which makes it extremely efficient to access sequentially (especially when you consider the effects of caching and prefetching of modern CPUs) or "randomly" trough a known integer index. Searching or inserting elements (especially in the middle) - not so much.

Dictionary is an associative data structure - a key can be anything hashable (not just integer index), but elements are not sorted in a "meaningful" way and the access through the known key is not as fast as List's integer index.

So, pick the right tool for the job.

which one is more preferable : dictionary or hashtable

In Java, neither is preferable IMHO. These are collections which were added in Java 1.0 and improved collections where added in Java 1.2 more than ten years ago.

For performance I would suggest you use

Map<Key, Value> map = new HashMap<Key, Value>();

List vs ArrayList vs Dictionary vs Hashtable vs Stack vs Queue?


Lists

Lists allow duplicate items, can be accessed by index, and support linear traversal.

  • ArrayList - An array-based list that doesn't support generic types. It does not enforce type safety and should generally be avoided.

  • List - An array list that supports generic types and enforces type-safety. Since it is non-contiguous, it can grow in size without re-allocating memory for the entire list. This is the more commonly used list collection.

Hashes

Hashes are look-ups in which you give each item in a list a "key" which will be used to retrieve it later. Think of a hash like a table index where you can ask questions like "I'm going to find this object by this string value. Duplicate keys are not allowed.

  • HashTable - A basic key-value-pair map that functions like an indexed list.

  • Dictionary - A hashtable that supports generic types and enforces type-safety.

Queues

Queues control how items in a list are accessed. You typically push/pop records from a queue in a particular direction (from either the front or back). Not used for random access in the middle.

  • Stack - A LIFO (last in, first out) list where you push/pop records on top of each other.

  • Queue - A FIFO (first in, first out) list where you push records on top and pop them off the bottom.

What is the primary difference between Dictionary and Hashtable


The Dictionary class differs from the
Hashtable class in more ways than one.
In addition to being strongly-typed,
the Dictionary also employs a
different collision resolution
strategy than the Hashtable class,
using a technique referred to as
chaining.

You can read this article: http://msdn.microsoft.com/en-us/library/ms379571(v=vs.80).aspx#datastructures20_2_topic6

It's really useful.

What are the differences b/w Hashtable, Dictionary and KeyValuePair?

Hashtable is an untyped associative container that uses DictionaryEntry class to return results of enumeration through its key-value pairs.

Dictionary<K,T> is a generic replacement of Hashtable that was introduced in C# 2.0. It uses KeyValuePair<K,T> generic objects to represent its key-value pairs.

The only place where you should see Hashtable these days is legacy code that must run on .NET 1.1, before generics have been introduced. It's been kept around for compatibility reasons, but you should prefer Dictionary<K,T> whenever you can.

When and where to Use Dictionary verses Hashtable?

HashTable is not a Generic Collection, so you can save anytype (object) types into Hashtable and its type is not known at compile time.

Dictionary is a Generic collection so you need to specify the type before saving elements into it, so it is type safe.

I want to know when Hash-table is preferred over Dictionary ?

i don't think this is true because Dictionary is Typesafe and also faster because of no burden of boxing and unboxing of the elements as its elements type is known at compile time.

so you should consider using Dictionary over Hash-Table



Related Topics



Leave a reply



Submit