Dictionaries
Hash Tables
Dictionary Class
HashSet and SortedSet
Advanced Data Collections
Key | Value |
---|---|
1 | Sofia |
2 | Plovdiv |
3 | Burgas |
4 | Ruse |
Very fast searching by key
O(1)
A hash table of length 10 uses open addressing with hash function h(k)=k mod 10, and linear probing. After inserting 6 values into an empty hash table, the table is as shown below. The numbers are: 46, 34, 42, 23, 52, 33
42 mod 10 = 2
34 mod 10 = 4
33 mod 10 = 3
NB: When the number of collisions is sufficiently small, the hash tables work quite well (fast)
Strategies
Chaining in a list:
Type of the key
Type of the value
(Try the performance when they return the same value for each item)
string text = "a text some text just some text";
Note: Write on paper
var dictionary = new Dictionary<int, string>();
dictionary.Add(1, "one");
dictionary.Add(2, "two");
dictionary.Add(3, "three");
var element = dictionary.ElementAt(1);
var dictionary = new SortedDictionary<int, string>();
dictionary.Add(2, "c");
dictionary.Add(1, "a");
dictionary.Add(3, "b");
class PriorityQueue<T> where T : IComparable<T>
{
private OrderedBag<T> queue;
public int Count
{
get { return this.queue.Count; }
}
public PriorityQueue()
{
this.queue = new OrderedBag<T>();
}
public void Enqueue(T element)
{
this.queue.Add(element);
}
public T Dequeue()
{
return this.queue.RemoveFirst();
}
}
var firstSet = new SortedSet<string>(new string[] { "Alabama", "Washington", "Colorado", "New York" });
var secondSet = new SortedSet<string>(new string[] { "New York", "Alaska", "Alabama" });
var union = new SortedSet<string>(firstSet);
union.UnionWith(secondSet);