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 question

Hashtable question

Scheduled Pinned Locked Moved C#
question
5 Posts 5 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.
  • V Offline
    V Offline
    Venkatraman
    wrote on last edited by
    #1

    Hey i am using a hashtable, and i am adding my fields collection to it. when i iterate through them they found to be not in the order in which i added them. how do i maintain the sequence in which the elements are added. thanks Cheers, Venkatraman Kalyanam Bangalore - India "Being Excellent is not a skill, it is an attitude" Reality is an illusion caused by caffeine deficiency(one Microsoft Research scholor)

    A J A 3 Replies Last reply
    0
    • V Venkatraman

      Hey i am using a hashtable, and i am adding my fields collection to it. when i iterate through them they found to be not in the order in which i added them. how do i maintain the sequence in which the elements are added. thanks Cheers, Venkatraman Kalyanam Bangalore - India "Being Excellent is not a skill, it is an attitude" Reality is an illusion caused by caffeine deficiency(one Microsoft Research scholor)

      A Offline
      A Offline
      Arjan Einbu
      wrote on last edited by
      #2

      You don't. Hashtables do not store the sequence that you added the items. You should use an arraylist.

      F 1 Reply Last reply
      0
      • A Arjan Einbu

        You don't. Hashtables do not store the sequence that you added the items. You should use an arraylist.

        F Offline
        F Offline
        Frank Olorin Rizzi
        wrote on last edited by
        #3

        Indeed. If you want your data to be always sorted, ArrayList is probably better than Hashtable. If you use Hashtable because most of the times you don't care about sorted data, but you'd rather have a near-O(1) access time, then Hashtable is better. Note, however, that you can use an Hashtable and, when needed, sort the values as needed and live happily ever-after. Frank

        1 Reply Last reply
        0
        • V Venkatraman

          Hey i am using a hashtable, and i am adding my fields collection to it. when i iterate through them they found to be not in the order in which i added them. how do i maintain the sequence in which the elements are added. thanks Cheers, Venkatraman Kalyanam Bangalore - India "Being Excellent is not a skill, it is an attitude" Reality is an illusion caused by caffeine deficiency(one Microsoft Research scholor)

          J Offline
          J Offline
          Jim Stewart
          wrote on last edited by
          #4

          You might consider the System.Collections.Specialized.ListDictionary. It implements the IDictionary interface with a linked list. It would be my guess (just a guess) that the order of addition is maintained. Be aware, according to the documentation, ListDictionary is superior to hashtable performance-wise up to about ten items. If your list is longer, consider another strategy.

          α.γεεκ

          Fortune passes everywhere.
          Duke Leto Atreides

          1 Reply Last reply
          0
          • V Venkatraman

            Hey i am using a hashtable, and i am adding my fields collection to it. when i iterate through them they found to be not in the order in which i added them. how do i maintain the sequence in which the elements are added. thanks Cheers, Venkatraman Kalyanam Bangalore - India "Being Excellent is not a skill, it is an attitude" Reality is an illusion caused by caffeine deficiency(one Microsoft Research scholor)

            A Offline
            A Offline
            Arun Bhalla
            wrote on last edited by
            #5

            For me, the best solution tends to be maintaining two data structures, an array or arraylist with the keys in the desired order, and a hashtable with the key-value pairs. You could easily devise data structures that combine the two, such as a hashtable where the list of keys is a linked list.

            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