The ConstainsKey
method is the way to know if a key already exists in the Dictionary.
This come in handy for data reduction. In the sample below, each time we encountner a new word, we add it as a key in the dictionary, else we increment the counter for this specific word.
Dim dic As IDictionary(Of String, Integer) = New Dictionary(Of String, Integer)
Dim words As String() = Split(<big text source>," ", -1, CompareMethod.Binary)
For Each str As String In words
If dic.ContainsKey(str) Then
dic(str) += 1
Else
dic.Add(str, 1)
End If
Next
XML reduction example : getting all the child nodes names and occurence in an branch of an XML document
Dim nodes As IDictionary(Of String, Integer) = New Dictionary(Of String, Integer)
Dim xmlsrc = New XmlDocument()
xmlsrc.LoadXml(<any text stream source>)
For Each xn As XmlNode In xmlsrc.FirstChild.ChildNodes 'selects the proper parent
If nodes.ContainsKey(xn.Name) Then
nodes(xn.Name) += 1
Else
nodes.Add(xn.Name, 1)
End If
Next