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 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
      • G George_George

        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 Offline
        A Offline
        Alan Balkany
        wrote on last edited by
        #24

        Here's link that explains more: http://en.wikipedia.org/wiki/Hash_table[^] Hashing transforms a key into the address of a slot to store the data in. There are a fixed number of slots. When multiple keys are mapped to the same slot, there are different ways of handling it, all of which take time. Type "hash table" into Google, and you'll get much more than I can explain here.

        G 1 Reply Last reply
        0
        • A Alan Balkany

          Here's link that explains more: http://en.wikipedia.org/wiki/Hash_table[^] Hashing transforms a key into the address of a slot to store the data in. There are a fixed number of slots. When multiple keys are mapped to the same slot, there are different ways of handling it, all of which take time. Type "hash table" into Google, and you'll get much more than I can explain here.

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

          How about my question item 1? -------------------- 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? -------------------- Fixed size means limited size? regards, George

          A 1 Reply Last reply
          0
          • G George_George

            How about my question item 1? -------------------- 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? -------------------- Fixed size means limited size? regards, George

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

            Look at the link I gave, type "hash table" into Google and look at some of those references, and see if they answer your questions. There's a lot of material on hash tables out there, with more complete explanations than I can give you here.

            G 1 Reply Last reply
            0
            • A Alan Balkany

              Look at the link I gave, type "hash table" into Google and look at some of those references, and see if they answer your questions. There's a lot of material on hash tables out there, with more complete explanations than I can give you here.

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

              Thanks Alan, I read the link, clarified a lot of things. I often heard people talk about re-hash, does it mean the same thing of "Table resizing" in the link you mentioned? regards, George

              A 1 Reply Last reply
              0
              • G George_George

                Thanks Alan, I read the link, clarified a lot of things. I often heard people talk about re-hash, does it mean the same thing of "Table resizing" in the link you mentioned? regards, George

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

                It might. I haven't heard that term in the context of hash tables.

                G 1 Reply Last reply
                0
                • A Alan Balkany

                  It might. I haven't heard that term in the context of hash tables.

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

                  Thanks Alan, Your reply is clear. I want to check why do you think before Hashtable is fixed size? If the load factor is large enough (e.g. HashMap in Java is 75%), it will be resized to increase its size -- so not fixed. Could you clarify please? regards, George

                  A 1 Reply Last reply
                  0
                  • G George_George

                    Thanks Alan, Your reply is clear. I want to check why do you think before Hashtable is fixed size? If the load factor is large enough (e.g. HashMap in Java is 75%), it will be resized to increase its size -- so not fixed. Could you clarify please? regards, George

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

                    If you increase the size of the hash table, you also have to change the hashing function to produce keys for this larger size. When you do this, the elements you've already added to the hash table must go to different slots. So every element must be re-inserted in the new larger table. This process creates a new hash table and replaces the old one. The old one wasn't resized.

                    G 1 Reply Last reply
                    0
                    • A Alan Balkany

                      If you increase the size of the hash table, you also have to change the hashing function to produce keys for this larger size. When you do this, the elements you've already added to the hash table must go to different slots. So every element must be re-inserted in the new larger table. This process creates a new hash table and replaces the old one. The old one wasn't resized.

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

                      Thanks Alan, So, the root cause is hash function is dependent on table size? :-) regards, George

                      A 1 Reply Last reply
                      0
                      • G George_George

                        Thanks Alan, So, the root cause is hash function is dependent on table size? :-) regards, George

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

                        Yes.

                        G 1 Reply Last reply
                        0
                        • A Alan Balkany

                          Yes.

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

                          Thanks Alan! You mentioned before -- "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.". "It" means? :-) regards, George

                          A 1 Reply Last reply
                          0
                          • G George_George

                            Thanks Alan! You mentioned before -- "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.". "It" means? :-) regards, George

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

                            Growing regularly. Every time the hashtable "grows", all elements that have been inserted must be reinserted into the new hashtable. When a hashtable has n elements, it takes n operations to copy them, as opposed to a single operation to insert one element.

                            G 1 Reply Last reply
                            0
                            • A Alan Balkany

                              Growing regularly. Every time the hashtable "grows", all elements that have been inserted must be reinserted into the new hashtable. When a hashtable has n elements, it takes n operations to copy them, as opposed to a single operation to insert one element.

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

                              Thanks Alan! Question answered. You are so patience. :-) regards, George

                              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