
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
628
|
Chapter 11: Data Structures and Algorithms
Items may be added to a MultiMap<T,U> object by running the code shown in
Example 11-9.
ContainsValue method Returns a bool indicating whether the MultiMap<T,U> contains a particular value. Its
syntax is:
ContainsValue(T value)
where value is the object to be found in the MultiMap<T,U>.
Remove method Removes a key from the MultiMap<T,U> and all its referent values in the internal map
Dictionary<T, List<U>>
. Its syntax is:
Remove(T key)
where key is the key to be removed.
Example 11-9. Testing the MultiMap class
public static void TestMultiMap( )
{
string s = "foo";
// Create and populate a MultiMap object.
MultiMap<int, string> myMap = new MultiMap<int, string>( );
myMap.Add(0, "zero");
myMap.Add(1, "one");
myMap.Add(2, "two");
myMap.Add(3, "three");
myMap.Add(3, "duplicate three");
myMap.Add(3, "duplicate three");
myMap.Add(4, "null");
myMap.Add(5, s);
myMap.Add(6, s);
// Display contents.
foreach (KeyValuePair<int, List<string>> entry in myMap)
{
Console.Write("Key: " + entry.Key.ToString( ) + "\tValue: ");
foreach (string str in myMap[entry.Key])
{
Console.Write(str + " : ");
}
Console.WriteLine( );
}
// Obtain values through the indexer.
Console.WriteLine( );
Console.WriteLine("((ArrayList) myMap[3])[0]: " + myMap[3][0]);
Console.WriteLine("((ArrayList) ...