![]() Though, again, if the data structure isn't one that works with your data ideally, you are given the tools (the interfaces) to be able to write one that works best for your data. The two classes have similar object models, and both have O(log n) retrieval. In this respect, it is similar to the SortedList generic class. The SortedDictionary generic class is a binary search tree with O(log n) retrieval, where n is the number of elements in the dictionary. You may find that the SortedDictionary is what you To this, you are perfectly capable of writing your own dictionary implementation that does things optimally for the data you have.Īs to the efficiency of various lookup/contains?īinary search of a sorted array: O(log N)įor most people, the hash table is what they want. In this case, that interface is IDictionary( docs). While the Dictionary is a general purpose solution you shouldn't be passing around concrete types (such as the Dictionary) - you should be passing around the interfaces. Hash table for some reason and you're left with linear probing). It is possible to get a pathological data set where the lookup devolves to O(N) (for example everything you insert falls to the same hash value or index in the You may wish to read the section on collision resolution. Note that I've linked the WikipediaĪrticle there - its a fairly good read. Its key is very fast, close to O(1), because the Dictionary class is implemented as a hash table.Īnd there we have it. Each addition to the dictionary consists of a value and its associated key. The Dictionary generic class provides a mapping from a set of keys to a set of values. If you dig down into the "Remarks" section: Table) or TreeMap (a sorted tree) (or ConcurrentSkipListMap . In C# - Its not as obvious as HashMap (a hash You've got to dig a bit to see how the Dictionary is implemented But concerning efficiency, what do they offer? What's the difference They include methods that help with many things and they combine strings with integers,(keys and values) and many more. But dictionaries are general.ĬontainsKey method is not specific to the data that it gets, it's a general searching method.īasically what I am asking is. If let's say I had created a specific hash function which corresponds to the type of data that I am having and is specifically designed for that set of data then yes, that hash function is indeed faster than looping through data. Using some more efficient way rather than looping through the keys and check if what I am searching exists? What's the difference in efficiency? Is the ContainsKey method Instead of using ContainsKey (orĪn equivalent method in another language) I loop through the ArrayList to check if something exists there (or performing binary search if data is sorted or something similar). Let's say instead of a dictionary I use an ArrayList. I have a question though on how they work. C# dictionaries are a simple way to find if something exists etc etc.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |