简体   繁体   English

带有两个键的字典条目 - VB.net

[英]Dictionary entries with two keys - VB.net

What would the .net experts recommend? .net 专家会推荐什么? I have a piece of data that I need to access using two different keys.我有一段数据需要使用两个不同的密钥访问。 Let's say the data looks like this:假设数据如下所示:

key1
key2
data

I need to add, find data by either key and delete the data using either key thousands of times and want to make it very quick.我需要添加,通过任一键查找数据并使用任一键删除数据数千次,并希望使其非常快。

I really like the clarity that LINQ adds to the code but...I have compared LINQ to dictionaries in loop retrieval situations.我真的很喜欢 LINQ 为代码添加的清晰性,但是……我已经将 LINQ 与循环检索情况中的字典进行了比较。 I don't like LINQ because it looks like it takes far more time to get any single piece of data.我不喜欢 LINQ,因为它看起来需要更多的时间来获取任何单个数据。 I like dictionaries because they are so fast with retrieval.我喜欢字典,因为它们的检索速度非常快。

I was thinking of writing a custom class that uses two dictionaries:我正在考虑编写一个使用两个字典的自定义类:

key1
data

and

key2
data

any time I add a data item to the instance of the class the class would need to add the data to two different backing dictionaries.每当我向类的实例添加数据项时,该类都需要将数据添加到两个不同的支持字典中。 Every time I remove a data item I would need to remove them from both backing dictionaries.每次我删除一个数据项时,我都需要从两个支持字典中删除它们。

Is this the best way to handle this issue or is there some fast "dictionary like" data structure in .net that allows me to have two keys for the same data?这是处理这个问题的最佳方法,还是 .net 中有一些快速的“类似字典”的数据结构,允许我对相同的数据有两个键?

Is this the best way to handle this issue or is there some fast "dictionary like" data structure in .net that allows me to have two keys for the same data?这是处理这个问题的最佳方法,还是 .net 中有一些快速的“类似字典”的数据结构,允许我对相同的数据有两个键?

There is no built in data structure that provides two keys, where either key can be used.没有提供两个键的内置数据结构,其中任何一个键都可以使用。

Given that you want to lookup by either key (and not both at once), using a custom class which encapsulates two Dictionary(Of TKey, Of TValue) makes sense, provided that the overhead of storing both dictionaries is not objectionable.鉴于您想通过任一键(而不是同时)进行查找,使用封装了两个Dictionary(Of TKey, Of TValue)的自定义类是有意义的,前提是存储两个字典的开销不会令人反感。

You can define a composite key eg您可以定义一个复合键,例如

Structure CKey
    Public Key1 As String
    Public Key2 As String
End Structure

And Dictionary that uses that key和使用该键的字典

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")

Then you can run a simple LINQ query like:然后你可以运行一个简单的 LINQ 查询,如:

Dim result = From el In myDict Where el.Key.Key1 = "key01" Select el

If you don't like LINQ you can still loop thru the dictionary in your own methods, checking the keys yourself.如果您不喜欢 LINQ,您仍然可以在自己的方法中遍历字典,自己检查键。

I'm really surprised that I got a solution here (because I am a noob to this kind of programming).我真的很惊讶我在这里得到了一个解决方案(因为我是这种编程的菜鸟)。

So there is two things to consider: You need to know which key you are using for the search and as well add and delete entries aside of each other.所以有两件事需要考虑:你需要知道你使用哪个键进行搜索,以及添加和删除彼此旁边的条目。 I guess you could do overcome these things with a shared sub for adding and deleting entries or maybe even with a class (don't know much about them).我想你可以用一个共享子来添加和删除条目或者甚至一个类(对它们不太了解)来克服这些问题。

Maybe someone can improve on that.也许有人可以改进这一点。

I did, see section below ;-]我做了,请参阅下面的部分;-]

    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

So I created a class for a dictionary with key1 as string, key2 as string and val as double with basic in and output operations:所以我为一个字典创建了一个类,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

A little late, but how about concatenate the key1 with the key 2 like:有点晚了,但是如何将 key1 与 key 2 连接起来,例如:

key1key2
data

With that, you can always get the data without all the fuss.有了它,您始终可以轻松获取数据。 You can add then like this:你可以像这样添加:

 Public DictVal As New Dictionary(Of String, String)

  DictVal.Add(key1 & key2, value)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM