When to use a HashTable

后端 未结 8 782
眼角桃花
眼角桃花 2020-12-24 02:13

In C#, I find myself using a List, IList or IEnumerable 99% of the time. Is there a case when it would be b

相关标签:
8条回答
  • 2020-12-24 02:43

    There's no way of telling exactly without knowing what the collection is for, but unless the items in your collection are unique you cannot use a hashtable, as there will be nothing to use as a key. So perhaps the rule of thumb you are looking for is that if your members are all different and you want to pull individual instances out by key, use a hashtable. If you have a bunch of items that you wish to treat in the same way (such as doing a foreach on the entire set) use a list.

    0 讨论(0)
  • 2020-12-24 02:47

    Use a hashtable, when you need to be able to (quickly) look up items by key.

    Of course, you can search through an IList or IEnumerable etc for a matching key but that will take O(n) time rather than O(1) for Hashtable or Dictionary.

    0 讨论(0)
  • 2020-12-24 02:49

    Maybe not directly related to the OPs question, but there's a useful blog post about which collection structure to use at: SortedSets

    Basically, what you want to do with the collection determines what type of collection you should create.

    To summarise in more detail:

    • Use IList if you want to be able to enumerate and / or modify the collection (normally adding at end of list)
    • Use IEnumeration if you just want to enumerate the collection (don't need to add / remove - usually used as a return type)
    • Use IDictionary if you want to access elements by a key (adding / removing elements quickly using a key)
    • Use SortedSet if you want to access a collection in a predefined order (most common usage being to access the collection in order)

    • Overall, use Dictionary if you want to access / modify items by key in no particular order (preferred over list as that's generally done in order, preferred over enumeration as you can't modify an enumeration, preferred over hashtable as that's not strictly typed, preferred over sortedlist when you don't need keys sorted)

    0 讨论(0)
  • 2020-12-24 02:50

    You use a hashtable (dictionary) when you want fast look up access to an item based on a key.

    If you are using List, IList or IEnumerable generally this means that you are looping over data (well in the case of IEnumerable it definitely means that), and a hashtable isn't going to net you anything. Now if you were looking up a value in one list and using that to access data in another list, that would a little different. For example:

    1. Find position in list of Item foo.
    2. Position in list for foo corresponds to position in another list which contains Foo_Value.
    3. Access position in seconds list to get Foo_Value.

    Here is a link describing the different datatypes.

    Another link.

    0 讨论(0)
  • 2020-12-24 02:51

    Hashtable optimizes lookups. It computes a hash of each key you add. It then uses this hash code to look up the element very quickly. It is an older .NET Framework type. It is slower than the generic Dictionary type.

    0 讨论(0)
  • 2020-12-24 02:57

    You're not really comparing the same things, when I use a dictionary it's because I want to have a lookup for the data, usually I want to store a list of objects and I want to be able to quickly look them up using a key of some kind.

    0 讨论(0)
提交回复
热议问题