MultiMap ClassImplement a MultiMap generic class that uses many values for each key.
This page was last reviewed on Nov 17, 2022.
MultiMap. A MultiMap has multiple values at a key. With it we add multiple values to a single key in a Dictionary. MultiMaps can be useful.
We see an example implementation of the MultiMap associative array in the C# language. We then use the class. This is not an ideal implementation—but it works.
An example. The base class library does not provide a MultiMap. This type is a Dictionary with values of type List. Here we show a MultiMap facade class.
Detail The MultiMap here uses a string key and any type of value. We add objects at specific keys.
Detail It provides a simple Add method. This method is implemented with TryGetValue.
Dictionary TryGetValue
Dictionary ContainsKey
Detail To continue, we have code add the value to the List. The value is always stored at its key, even with duplicates.
Detail The MultiMap exposes Keys. You will want to enumerate over Keys in your code.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; public class MultiMap<V> { Dictionary<string, List<V>> _dictionary = new Dictionary<string, List<V>>(); public void Add(string key, V value) { // Add a key. List<V> list; if (this._dictionary.TryGetValue(key, out list)) { list.Add(value); } else { list = new List<V>(); list.Add(value); this._dictionary[key] = list; } } public IEnumerable<string> Keys { get { // Get all keys. return this._dictionary.Keys; } } public List<V> this[string key] { get { // Get list at a key. List<V> list; if (!this._dictionary.TryGetValue(key, out list)) { list = new List<V>(); this._dictionary[key] = list; } return list; } } } class Program { static void Main() { // Create first MultiMap. var multiMap = new MultiMap<bool>(); multiMap.Add("key1", true); multiMap.Add("key1", false); multiMap.Add("key2", false); foreach (string key in multiMap.Keys) { foreach (bool value in multiMap[key]) { Console.WriteLine("MULTIMAP: " + key + "=" + value); } } // Create second MultiMap. var multiMap2 = new MultiMap<string>(); multiMap2.Add("animal", "cat"); multiMap2.Add("animal", "dog"); multiMap2.Add("human", "tom"); multiMap2.Add("human", "tim"); multiMap2.Add("mineral", "calcium"); foreach (string key in multiMap2.Keys) { foreach (string value in multiMap2[key]) { Console.WriteLine("MULTIMAP2: " + key + "=" + value); } } } }
MULTIMAP: key1=True MULTIMAP: key1=False MULTIMAP: key2=False MULTIMAP2: animal=cat MULTIMAP2: animal=dog MULTIMAP2: human=tom MULTIMAP2: human=tim MULTIMAP2: mineral=calcium
Notes, indexer. The MultiMap provides an indexer. This provides natural and short syntax for accessing the MultiMap. It returns an empty List if nothing exists.
Notes, example programs. The Main() method shows how to use the MultiMap to store bool and string values. It is fairly simple to figure out, and can be extended in many ways.
A summary. We saw an implementation of the MultiMap class in the C# language. If you don't need a complex class, it is best to use the minimum that meets your needs.
The facade here simplifies Dictionary and wraps it with MultiMap features. It is a Dictionary of Lists, inside a generic wrapper class.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Nov 17, 2022 (simplify).
© 2007-2024 Sam Allen.