Home
VB.NET
MultiMap Class
Updated Jul 20, 2024
Dot Net Perls
MultiMap. Sometimes we want to map one String to a single String, but more often, we may want to map one String to many Strings. This is a MultiMap, and it can be implemented in VB.NET code.
With a Dictionary field, and some subroutines and properties, we can create a simple MultiMap class. Then we can reuse this class for mapping one key to multiple values.
Dictionary
Class
Example. We introduce the MultiMap class, which contains a Dictionary field. We then have the Add Function, a Keys property, and an indexer (a This property).
Part 1 We create a MultiMap instance and call Add several times. Each key is combined, but different values are keys for each key.
Part 2 When we access the Keys property, we can loop over the String keys in an IEnumerable.
IEnumerable
Property
Part 3 We use the "This" Property (the Default property) and access the List of values at a String key.
List
Class MultiMap Dim _dictionary as Dictionary(Of String, List(Of String)) = New Dictionary(Of String, List(Of String)) Public Sub Add(key As String, value As String) ' Add a String key. ' For the list of values, add a value onto the end of the List if one exists. Dim list as List(Of String) = Nothing If _dictionary.TryGetValue(key, list) list.Add(value) Else list = New List(Of String) list.Add(value) _dictionary(key) = list End If End Sub Public ReadOnly Property Keys As IEnumerable(Of String) Get ' Get all keys. Return _dictionary.Keys End Get End Property Public ReadOnly Default Property This(key As String) As List(Of String) Get ' Get list at a key. Dim list As List(Of String) = Nothing ' Create empty list if none exists. If Not _dictionary.TryGetValue(key, list) list = New List(Of String) _dictionary(key) = list End If Return list End Get End Property End Class Module Module1 Sub Main() ' Part 1: Create MultiMap by calling Add Function. Dim multiMap = New MultiMap() multiMap.Add("animal", "cat") multiMap.Add("animal", "dog") multiMap.Add("color", "blue") multiMap.Add("color", "orange") multiMap.Add("mineral", "calcium") ' Part 2: Loop over keys in MultiMap with property. For Each key in multiMap.Keys ' Part 3: Access all values for a specific key, and print them. For Each value in multiMap(key) Console.WriteLine($"MULTIMAP: {key} = {value}") Next Next End Sub End Module
MULTIMAP: animal = cat MULTIMAP: animal = dog MULTIMAP: color = blue MULTIMAP: color = orange MULTIMAP: mineral = calcium
Property syntax. In VB.NET, we must use the ReadOnly keyword to describe a Property that only has a Get function. And for an indexer (This) we must use the Default keyword.
Summary. Though the MultiMap implementation here is simple, it could be useful in VB.NET programs that need to map unique Strings to multiple String values. Generic types could be added.
Generic
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Jul 20, 2024 (edit link).
Home
Changes
© 2007-2025 Sam Allen