
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
630
|
Chapter 11: Data Structures and Algorithms
Key: 6 Value: foo :
((ArrayList) myMap[3])[0]: three
((ArrayList) myMap[3])[1]: duplicate three
myMap.Count: 6
entry.Key: 2 entry.Value(s): two :
entry.Key: 3 entry.Value(s): three : duplicate three : duplicate three :
entry.Key: 4 entry.Value(s):
entry.Key: 5 entry.Value(s): foo :
entry.Key: 6 entry.Value(s): foo :
entry.Key: 10 entry.Value(s): BAR : BAZ :
myMap.ContainsKey(2): True
myMap.ContainsValue(two): True
Contains Key 2: True
Contains Key 12: False
Contains Value two: True
Contains Value BAR: True
Discussion
A one-to-many map, or multimap, allows one object, a key, to be associated, or
mapped, to zero or more objects. The
MultiMap<T,U> class presented here operates
similarly to a
Dictionary<T,U>. The MultiMap<T,U> class contains a Dictionary<T,
List<U>>
field called map that contains the actual mapping of keys to values. Several
of the
MultiMap<T,U> methods are delegated to the methods on the map Dictionary<T,
List<U>>
object.
A
Dictionary<T,U> operates on a one-to-one principle: only one key may be associ-
ated with one value at any time. However, if you need to associate multiple values
with a single key, you must use the approach used by the
MultiMap<T,U> class. The
private
map field associates a key with a single List<U> of values, which allows ...