Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Hashtable v.s. Dictionary

Hashtable v.s. Dictionary

Scheduled Pinned Locked Moved C#
question
35 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G George_George

    V1 means C# 1.0 or? regards, George

    F Offline
    F Offline
    Francofu
    wrote on last edited by
    #4

    V1 should mean Version1.0 or Version1.1 V2 use dictionary,me too

    G 1 Reply Last reply
    0
    • G George_George

      V1 means C# 1.0 or? regards, George

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #5

      .net V1

      G 1 Reply Last reply
      0
      • P PIEBALDconsult

        .net V1

        G Offline
        G Offline
        George_George
        wrote on last edited by
        #6

        Thanks PIEBALDconsult, Question answered. regards, George

        1 Reply Last reply
        0
        • F Francofu

          V1 should mean Version1.0 or Version1.1 V2 use dictionary,me too

          G Offline
          G Offline
          George_George
          wrote on last edited by
          #7

          Cool, Chao.Fu! Question answered. regards, George

          1 Reply Last reply
          0
          • G George_George

            Hello everyone, Any guidelines about when to use Hashtable and when to use Dictionary? I always use Dictionary. :-) thanks in advance, George

            T Offline
            T Offline
            telha
            wrote on last edited by
            #8

            Well MSDN states that Dictionary(Of TKey, of tValue) is a new class in v2.0. but further explains that the Objects used as Key are required to have an implementation of Equality Comparer, IEqualityComparer whereas in HashTable the objects used as Keys are required to override the IHashCode Provider interface or Object.GetHashCode method. Internally HashTable uses the Dictionary object as its Key/Value pair but hashtable is faster in retrieval. Rest of the details are well explained in MSDN.

            Muhammad Talha

            G G 2 Replies Last reply
            0
            • T telha

              Well MSDN states that Dictionary(Of TKey, of tValue) is a new class in v2.0. but further explains that the Objects used as Key are required to have an implementation of Equality Comparer, IEqualityComparer whereas in HashTable the objects used as Keys are required to override the IHashCode Provider interface or Object.GetHashCode method. Internally HashTable uses the Dictionary object as its Key/Value pair but hashtable is faster in retrieval. Rest of the details are well explained in MSDN.

              Muhammad Talha

              G Offline
              G Offline
              Guffa
              wrote on last edited by
              #9

              telha wrote:

              but hashtable is faster in retrieval

              Do you have any source to back that up? The HashTable has a lot of additional code for handling thread safety, which of course slows down the process a bit, but other than that they seem to work pretty much the same. Besides, once you have retrieved the value from the HashTable, you have to cast it, which you don't need to do with a Dictionary.

              Despite everything, the person most likely to be fooling you next is yourself.

              G 1 Reply Last reply
              0
              • G George_George

                Hello everyone, Any guidelines about when to use Hashtable and when to use Dictionary? I always use Dictionary. :-) thanks in advance, George

                A Offline
                A Offline
                Alan Balkany
                wrote on last edited by
                #10

                A Dictionary IS implemented as a hash table: http://msdn.microsoft.com/en-us/library/xfhwa508(VS.80).aspx[^] So if you're using a Dictionary, you're using a hash table.

                G 1 Reply Last reply
                0
                • G Guffa

                  telha wrote:

                  but hashtable is faster in retrieval

                  Do you have any source to back that up? The HashTable has a lot of additional code for handling thread safety, which of course slows down the process a bit, but other than that they seem to work pretty much the same. Besides, once you have retrieved the value from the HashTable, you have to cast it, which you don't need to do with a Dictionary.

                  Despite everything, the person most likely to be fooling you next is yourself.

                  G Offline
                  G Offline
                  George_George
                  wrote on last edited by
                  #11

                  Thanks Guffa,

                  Guffa wrote:

                  The HashTable has a lot of additional code for handling thread safety

                  Do you mean Hashtable is a class of thread safety? regards, George

                  G 1 Reply Last reply
                  0
                  • A Alan Balkany

                    A Dictionary IS implemented as a hash table: http://msdn.microsoft.com/en-us/library/xfhwa508(VS.80).aspx[^] So if you're using a Dictionary, you're using a hash table.

                    G Offline
                    G Offline
                    George_George
                    wrote on last edited by
                    #12

                    Thanks Alan, I think it is more correct to say Hashtable is a special type of Dictionary, whose key type is Object, correct? regards, George

                    A 1 Reply Last reply
                    0
                    • T telha

                      Well MSDN states that Dictionary(Of TKey, of tValue) is a new class in v2.0. but further explains that the Objects used as Key are required to have an implementation of Equality Comparer, IEqualityComparer whereas in HashTable the objects used as Keys are required to override the IHashCode Provider interface or Object.GetHashCode method. Internally HashTable uses the Dictionary object as its Key/Value pair but hashtable is faster in retrieval. Rest of the details are well explained in MSDN.

                      Muhammad Talha

                      G Offline
                      G Offline
                      George_George
                      wrote on last edited by
                      #13

                      Thanks Muhammad, Happy to learn new things from you. Two more comments, 1.

                      telha wrote:

                      Well MSDN states that Dictionary(Of TKey, of tValue) is a new class in v2.0. but further explains that the Objects used as Key are required to have an implementation of Equality Comparer, IEqualityComparer whereas in HashTable the objects used as Keys are required to override the IHashCode Provider interface or Object.GetHashCode method.

                      I did some search from MSDN, I think Dictionary is using either IEqualityComparer.Equals or System.IEquatable, while Hashtable is using Object.GetHashCode to check whether two keys' are the same? 2. From, http://msdn.microsoft.com/en-us/library/xfhwa508(VS.80).aspx[^] What means "Every key in a Dictionary must be unique according to the dictionary's equality comparer" -- I understand key in a Dictionary should have an equality comparer, but I do not know what means an equality comparer for a Dictionary itself? regards, George

                      G 1 Reply Last reply
                      0
                      • G George_George

                        Thanks Guffa,

                        Guffa wrote:

                        The HashTable has a lot of additional code for handling thread safety

                        Do you mean Hashtable is a class of thread safety? regards, George

                        G Offline
                        G Offline
                        Guffa
                        wrote on last edited by
                        #14

                        Yes, HasTable is a synchronised collection, while Dictionary isn't.

                        Despite everything, the person most likely to be fooling you next is yourself.

                        G 1 Reply Last reply
                        0
                        • G Guffa

                          Yes, HasTable is a synchronised collection, while Dictionary isn't.

                          Despite everything, the person most likely to be fooling you next is yourself.

                          G Offline
                          G Offline
                          George_George
                          wrote on last edited by
                          #15

                          Thanks Guffa, It should be a big difference. :-) Any comments to? http://www.codeproject.com/script/Forums/View.aspx?fid=1649&msg=2588727[^] regards, George

                          1 Reply Last reply
                          0
                          • G George_George

                            Thanks Alan, I think it is more correct to say Hashtable is a special type of Dictionary, whose key type is Object, correct? regards, George

                            A Offline
                            A Offline
                            Alan Balkany
                            wrote on last edited by
                            #16

                            I think it's the other way around. A hashtable is a specific type of data structure whose key can be anything that can be represented as binary. A Dictionary is an object that performs a function, and it can be implemented with a hashtable or other data structures. Often you see this functionality implemented with a red-black tree which can grow dynamically (e.g. in C++'s STL), as opposed to a hashtable which has a fixed size.

                            G 1 Reply Last reply
                            0
                            • G George_George

                              Thanks Muhammad, Happy to learn new things from you. Two more comments, 1.

                              telha wrote:

                              Well MSDN states that Dictionary(Of TKey, of tValue) is a new class in v2.0. but further explains that the Objects used as Key are required to have an implementation of Equality Comparer, IEqualityComparer whereas in HashTable the objects used as Keys are required to override the IHashCode Provider interface or Object.GetHashCode method.

                              I did some search from MSDN, I think Dictionary is using either IEqualityComparer.Equals or System.IEquatable, while Hashtable is using Object.GetHashCode to check whether two keys' are the same? 2. From, http://msdn.microsoft.com/en-us/library/xfhwa508(VS.80).aspx[^] What means "Every key in a Dictionary must be unique according to the dictionary's equality comparer" -- I understand key in a Dictionary should have an equality comparer, but I do not know what means an equality comparer for a Dictionary itself? regards, George

                              G Offline
                              G Offline
                              Guffa
                              wrote on last edited by
                              #17

                              George_George wrote:

                              I did some search from MSDN, I think Dictionary is using either IEqualityComparer.Equals or System.IEquatable, while Hashtable is using Object.GetHashCode to check whether two keys' are the same?

                              Both the HashTable and the Dictionary uses both the GetHashCode method and some kind of comparer. The hash code is used to determine a bucket for the item, and the comparer is used when looping through the items in the bucket to find the correct item. HashTable uses the Equals method if no customer key comparer is specified. Dictionary uses the default EqualityComparer for the key if no custom key comparer is specified.

                              George_George wrote:

                              What means "Every key in a Dictionary must be unique according to the dictionary's equality comparer" -- I understand key in a Dictionary should have an equality comparer, but I do not know what means an equality comparer for a Dictionary itself?

                              That's the same thing. The equality comparer for the dictionary is only used to compare keys.

                              Despite everything, the person most likely to be fooling you next is yourself.

                              G 1 Reply Last reply
                              0
                              • A Alan Balkany

                                I think it's the other way around. A hashtable is a specific type of data structure whose key can be anything that can be represented as binary. A Dictionary is an object that performs a function, and it can be implemented with a hashtable or other data structures. Often you see this functionality implemented with a red-black tree which can grow dynamically (e.g. in C++'s STL), as opposed to a hashtable which has a fixed size.

                                G Offline
                                G Offline
                                George_George
                                wrote on last edited by
                                #18

                                Thanks Alan,

                                Alan Balkany wrote:

                                as opposed to a hashtable which has a fixed size.

                                You mean Hashtable does not grow? I think when we insert elements into Hashtable, it grows. :-) Any comments? regards, George

                                A 1 Reply Last reply
                                0
                                • G Guffa

                                  George_George wrote:

                                  I did some search from MSDN, I think Dictionary is using either IEqualityComparer.Equals or System.IEquatable, while Hashtable is using Object.GetHashCode to check whether two keys' are the same?

                                  Both the HashTable and the Dictionary uses both the GetHashCode method and some kind of comparer. The hash code is used to determine a bucket for the item, and the comparer is used when looping through the items in the bucket to find the correct item. HashTable uses the Equals method if no customer key comparer is specified. Dictionary uses the default EqualityComparer for the key if no custom key comparer is specified.

                                  George_George wrote:

                                  What means "Every key in a Dictionary must be unique according to the dictionary's equality comparer" -- I understand key in a Dictionary should have an equality comparer, but I do not know what means an equality comparer for a Dictionary itself?

                                  That's the same thing. The equality comparer for the dictionary is only used to compare keys.

                                  Despite everything, the person most likely to be fooling you next is yourself.

                                  G Offline
                                  G Offline
                                  George_George
                                  wrote on last edited by
                                  #19

                                  Great Guffa! 1.

                                  Guffa wrote:

                                  The hash code is used to determine a bucket for the item, and the comparer is used when looping through the items in the bucket to find the correct item.

                                  If more then one elements falls into the same bucket (I think it means hashing conflicting), from your reply, it seems the elements of the same bucket is sorted using Comparer? 2.

                                  Guffa wrote:

                                  That's the same thing. The equality comparer for the dictionary is only used to compare keys.

                                  From the Key type, we can implement the type from equality class, but how to assign the equality cmoparer for the Dictionary itself? regards, George

                                  G 1 Reply Last reply
                                  0
                                  • G George_George

                                    Great Guffa! 1.

                                    Guffa wrote:

                                    The hash code is used to determine a bucket for the item, and the comparer is used when looping through the items in the bucket to find the correct item.

                                    If more then one elements falls into the same bucket (I think it means hashing conflicting), from your reply, it seems the elements of the same bucket is sorted using Comparer? 2.

                                    Guffa wrote:

                                    That's the same thing. The equality comparer for the dictionary is only used to compare keys.

                                    From the Key type, we can implement the type from equality class, but how to assign the equality cmoparer for the Dictionary itself? regards, George

                                    G Offline
                                    G Offline
                                    Guffa
                                    wrote on last edited by
                                    #20

                                    George_George wrote:

                                    If more then one elements falls into the same bucket (I think it means hashing conflicting), from your reply, it seems the elements of the same bucket is sorted using Comparer?

                                    The elements are not sorted, they are just placed in buckets and the order in a bucket is not defined. The comparer is always used when retrieving items. First the bucket is determined from the hash code, then the comparer is used to compare the desired key with the keys of the stored items (even if there is only one in the bucket).

                                    George_George wrote:

                                    From the Key type, we can implement the type from equality class, but how to assign the equality cmoparer for the Dictionary itself?

                                    You can specify a comparer in the constructor to the dictionary. I actually wrote an article about that: Dictionary with a Custom Key[^]

                                    Despite everything, the person most likely to be fooling you next is yourself.

                                    G 1 Reply Last reply
                                    0
                                    • G George_George

                                      Thanks Alan,

                                      Alan Balkany wrote:

                                      as opposed to a hashtable which has a fixed size.

                                      You mean Hashtable does not grow? I think when we insert elements into Hashtable, it grows. :-) Any comments? regards, George

                                      A Offline
                                      A Offline
                                      Alan Balkany
                                      wrote on last edited by
                                      #21

                                      A hashtable has a fixed size. When it gets too full its performance degrades, so one strategy is to then create a bigger one, and reinsert all the elements into it. This has some overhead. Normally a hashtable allows insertion and retrieval in constant time. When the hashtable has to grow regularly, it can increase the time for insertion to O(n) or higher, depending on how often it happens.

                                      G 1 Reply Last reply
                                      0
                                      • G Guffa

                                        George_George wrote:

                                        If more then one elements falls into the same bucket (I think it means hashing conflicting), from your reply, it seems the elements of the same bucket is sorted using Comparer?

                                        The elements are not sorted, they are just placed in buckets and the order in a bucket is not defined. The comparer is always used when retrieving items. First the bucket is determined from the hash code, then the comparer is used to compare the desired key with the keys of the stored items (even if there is only one in the bucket).

                                        George_George wrote:

                                        From the Key type, we can implement the type from equality class, but how to assign the equality cmoparer for the Dictionary itself?

                                        You can specify a comparer in the constructor to the dictionary. I actually wrote an article about that: Dictionary with a Custom Key[^]

                                        Despite everything, the person most likely to be fooling you next is yourself.

                                        G Offline
                                        G Offline
                                        George_George
                                        wrote on last edited by
                                        #22

                                        Hi Guffa, I take some time to study your article. It is not big, but clear enough, cool! I like it! :-) A few comments, 1. Why you use inner class to implement the comparer interface? Why not just let the parking special key class implements the comparer interface directly? Any benefits? 2. I want to confirm with you that, when we use Hashtable, Object.Equals and Object.GetHashCode is used. But when we use Dictionary, IEqualityComparer.Equals and IEqualityComparer.GetHashCode is used? 3. Equality comparer of the Dictionary is what is used as the 1st parameter of the Dictonary constructor, like your inner class ParkingSpaceKey.EqualityComparer? regards, George

                                        1 Reply Last reply
                                        0
                                        • A Alan Balkany

                                          A hashtable has a fixed size. When it gets too full its performance degrades, so one strategy is to then create a bigger one, and reinsert all the elements into it. This has some overhead. Normally a hashtable allows insertion and retrieval in constant time. When the hashtable has to grow regularly, it can increase the time for insertion to O(n) or higher, depending on how often it happens.

                                          G Offline
                                          G Offline
                                          George_George
                                          wrote on last edited by
                                          #23

                                          Thanks Alan, 1.

                                          Alan Balkany wrote:

                                          A hashtable has a fixed size. When it gets too full its performance degrades, so one strategy is to then create a bigger one, and reinsert all the elements into it.

                                          I am confused what do you mean fixed size. I think from developer point of view, we can insert as much elements as possible into Hashtable, so no feel of limitation of fixed size. Any more descriptions or pseudo code please? 2.

                                          Alan Balkany wrote:

                                          When the hashtable has to grow regularly, it can increase the time for insertion to O(n) or higher, depending on how often it happens.

                                          What means grow regularly? Why the performance degrades? Could you provide more description please? (reference document link is also ok, I am interested in this topic) regards, George

                                          A 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups