[英]Dictionary entries with two keys - VB.net
.net 專家會推薦什么? 我有一段數據需要使用兩個不同的密鑰訪問。 假設數據如下所示:
key1
key2
data
我需要添加,通過任一鍵查找數據並使用任一鍵刪除數據數千次,並希望使其非常快。
我真的很喜歡 LINQ 為代碼添加的清晰性,但是……我已經將 LINQ 與循環檢索情況中的字典進行了比較。 我不喜歡 LINQ,因為它看起來需要更多的時間來獲取任何單個數據。 我喜歡字典,因為它們的檢索速度非常快。
我正在考慮編寫一個使用兩個字典的自定義類:
key1
data
和
key2
data
每當我向類的實例添加數據項時,該類都需要將數據添加到兩個不同的支持字典中。 每次我刪除一個數據項時,我都需要從兩個支持字典中刪除它們。
這是處理這個問題的最佳方法,還是 .net 中有一些快速的“類似字典”的數據結構,允許我對相同的數據有兩個鍵?
這是處理這個問題的最佳方法,還是 .net 中有一些快速的“類似字典”的數據結構,允許我對相同的數據有兩個鍵?
沒有提供兩個鍵的內置數據結構,其中任何一個鍵都可以使用。
鑒於您想通過任一鍵(而不是同時)進行查找,使用封裝了兩個Dictionary(Of TKey, Of TValue)
的自定義類是有意義的,前提是存儲兩個字典的開銷不會令人反感。
您可以定義一個復合鍵,例如
Structure CKey
Public Key1 As String
Public Key2 As String
End Structure
和使用該鍵的字典
Dim myDict As New Dictionary(Of CKey, String)
myDict.Add(New CKey() With {.Key1 = "key01", .Key2 = "key02"}, "Data0102")
myDict.Add(New CKey() With {.Key1 = "key11", .Key2 = "key12"}, "Data0103")
myDict.Add(New CKey() With {.Key1 = "key01", .Key2 = "key22"}, "Data0104")
myDict.Add(New CKey() With {.Key1 = "key11", .Key2 = "key22"}, "Data0105")
然后你可以運行一個簡單的 LINQ 查詢,如:
Dim result = From el In myDict Where el.Key.Key1 = "key01" Select el
如果您不喜歡 LINQ,您仍然可以在自己的方法中遍歷字典,自己檢查鍵。
我真的很驚訝我在這里得到了一個解決方案(因為我是這種編程的菜鳥)。
所以有兩件事需要考慮:你需要知道你使用哪個鍵進行搜索,以及添加和刪除彼此旁邊的條目。 我想你可以用一個共享子來添加和刪除條目或者甚至一個類(對它們不太了解)來克服這些問題。
也許有人可以改進這一點。
我做了,請參閱下面的部分;-]
Dim myDict As New Dictionary(Of String, Double)
Dim myKeyDict As New Dictionary(Of String, String)
myDict.Add("key1", 5.5)
myKeyDict.Add("key2", "key1")
' Access Dictionary with key1
myDict("key1") = 7.7
Debug.Print(CStr(myDict("key1")))
' Acces Dictionary with key2
Dim thisKey1 As String = myKeyDict("key2") ' retrieve key1 with the help of key2
Debug.Print(CStr(myDict(thisKey1))) ' Acces the actuall dictionary with the retrieved key 1
所以我為一個字典創建了一個類,key1 為字符串,key2 為字符串,val 為 double,具有基本的輸入和輸出操作:
Public Class MultiKeyDictonaryDbl
Public DictVal As New Dictionary(Of String, Double) ' key1=Variablenname, val=Wert
Public DictKey1 As New Dictionary(Of String, String) ' key1=In/Output-Variablenname (Textdatei), key2=Variablenname
Public DictKey2 As New Dictionary(Of String, String) ' key2=In/Output-Variablenname (Textdatei), key1=Variablenname
Public length As Integer
Public Sub Add(key1 As String, key2 As String, val As Double)
DictVal.Add(key1, val)
DictKey1.Add(key1, key2)
DictKey2.Add(key2, key1)
End Sub
Public Sub Remove(key As String, id As Integer)
Dim key1 As String = Nothing
Dim key2 As String = Nothing
Dim chk As Boolean
If id = 1 Then
key1 = key
chk = DictKey1.TryGetValue(key1, key2)
ElseIf id = 2 Then
key2 = key
chk = DictKey2.TryGetValue(key2, key1)
End If
If chk = True Then
DictVal.Remove(key1)
DictKey1.Remove(key1)
DictKey2.Remove(key2)
End If
End Sub
Public Function getValue(key As String, id As Integer) As Double
Dim key1 As String = Nothing
Dim key2 As String = Nothing
Dim chk As Boolean
If id = 1 Then
key1 = key : chk = True
ElseIf id = 2 Then
key2 = key
chk = DictKey2.TryGetValue(key2, key1)
End If
If chk = True Then
chk = DictVal.TryGetValue(key1, getValue)
End If
If chk = False Then getValue = Double.PositiveInfinity
End Function
Public Function getList() As String(,)
Dim val As Double
Dim key1 As String = Nothing
Dim key2 As String = Nothing
Dim i As Integer = -1
' getLength in one line of code
length = -1 : Dim l1 As Integer = DictVal.Count : Dim l2 As Integer = DictKey1.Count : Dim l3 As Integer = DictKey2.Count : If l1 = l2 And l2 = l3 Then length = l1
If length < 1 Then Exit Function
Dim List(length - 1, 2) As String
For Each ele In DictKey2
i += 1
key2 = ele.Key : key1 = DictKey2(key2) : val = DictVal(key1)
List(i, 0) = key1 : List(i, 1) = key2 : List(i, 2) = CStr(val)
Next
getList = List
End Function
Public Function getLength() As Integer
getLength = -1
Dim l1 As Integer = DictVal.Count
Dim l2 As Integer = DictKey1.Count
Dim l3 As Integer = DictKey2.Count
If l1 = l2 And l2 = l3 Then getLength = l1
length = getLength
End Function
End Class
Sub testDictionaryVariablenVerarbeitung()
' some tests
Dim testit As New MultiKeyDictonaryDbl
testit.Add("Variablenname", "In/Output-Variablenname", 55.7)
testit.Add("Variablenname2", "In/Output-Variablenname2", 90.7)
Debug.Print(CStr(testit.getLength()))
testit.Add("Blub", "dabdi", 916)
testit.Remove("Variablenname", 1)
Dim liste(,) As String = testit.getList
Debug.Print(CStr(testit.getValue("Variablenname2", 1)))
Debug.Print(CStr(testit.getValue("dabdi", 2)))
Debug.Print(CStr(testit.getValue("dabdi", 1)))
End Sub
有點晚了,但是如何將 key1 與 key 2 連接起來,例如:
key1key2
data
有了它,您始終可以輕松獲取數據。 你可以像這樣添加:
Public DictVal As New Dictionary(Of String, String)
DictVal.Add(key1 & key2, value)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.