Friday, 2 November 2007

Chapter 4, lesson3: Dictionaries.

Dictionaries - opposite end of spectrum to sequental lists. Stores key/value pairs, to allow lookup of values based on key.

Hashtable

  • Add() - specify key, specify value.
  • Indexer - key in the indexer, and then assign value.
  • retrieval - ~ indexer.
  • DictionaryEntry - derived from ICollection - a container containing Key and Value. foreach( DictionaryEntry entry in ) .Value;
  • Object.GetHashCode() - returns an int that uniquely identifies the object.
  • Hashtable - allows only unique hashes of keys, not unique keys.
  • Two strings with same text have same hash, even though they are diff. instances.
  • Hashtable - tests for equality by testing the hash codes of keys. If equal, it will then call Equals mthd, to see whether 2 objs are indeed equal.
  • Consider overriding - GetHashCode, Equals. Thus one can change a class to provide equality.
  • ...and if changing the class to provide equality is difficult? e.g. if want to store strings as keys?? - provide equality OUTSIDE the class.
  • equality OUTSIDE the class - hashtable can use a class that calculates equality.
  • Hashtable - overloaded constructor accepts instance of IEqulaityComparer interface as arg.
  • IEqualityComparer - impl. GetHashCode(), Equals(). Mthds allow the Comparer Class to handle equality re. keys, instead of relying on the key class to supply them.
  • iterating over a hashtable - returns item sin order of their hsah value.
  • If need sorting use SortedList...
SortedList
  • SortedLIst - a dictionary class.
  • can access stored items in order.
  • supports key/value access by index.
  • performs sorting as items are added.
  • Can supply a Comparer class to constructor to control how sorting is performed.

ListDictionary

  • Use like hashtable but for small (<10)>
  • If colln will fluctuate bet large & small, use HybridDictionary.
  • OrderedDictionary - like a hashtable except it allows access to items by index.

No comments: