创建一个以键为列表的字典(字符串) [英] create a dictionary with key as list(of string)
问题描述
需要创建一个字典,其中的键是列表(字符串)或字符串数组
need to create a dictionary where keys are list(Of String) or array of string
我找到了此链接 将C#列表作为字典键
但是我需要帮助来了解如何使用列表(字符串)
but i need an help to understand how to do it with list(of String)
Class test
Sub TryTest()
myDict.Add(New List(Of String) From {"Huey", "Dewey"}, actions.Sleeping)
myDict.Add(New List(Of String) From {"Dewey", "Louie"}, actions.Playing)
Dim newList As New List(Of String) From {"Dewey", "Louie"}
If myDict.ContainsKey(newList) Then
MsgBox("myDict contains the list of String, as Key Value")
Else
MsgBox("myDict don't contains the list of String, as Key Value")
End If
End Sub
Dim myDict As New Dictionary(Of List(Of String), actions)
End Class
Enum actions
Sleeping
Eating
Studying
Playing
End Enum
我希望包含键的字典输出.
I expected that the dictionary output that contains the key.
P.S.由于c#与vb.net十分接近,并且在网上有很多c#/vb.net转换器可以轻松翻译,因此,也欢迎c#帮助.
P.S. Since c# it's close to vb.net, and on the net there are lot's of c#/vb.net translators that translate easily, please, also c# help are appreciated.
更新(在 Jeppe Stig Nielsen 帮助之后,我试图实现一个继承EqualityComparer的类,但是它不起作用...也许我在语法上犯了错误...有人知道我的方法有什么问题吗?
UPDATE (after Jeppe Stig Nielsen helps, i tried to implement a class that inherits EqualityComparer, but it doesn't work... maybe i mistake something in syntax... do someone know what's wrong in my approach?
Class test
Sub TryTest()
myDict.Add(New List(Of String) From {"Huey", "Dewey"}, actions.Sleeping)
myDict.Add(New List(Of String) From {"Dewey", "Louie"}, actions.Playing)
Dim newList As New List(Of String) From {"Dewey", "Louie"}
If myDict.ContainsKey(newList) Then
MsgBox("myDict contains the list of String, as Key Value")
Else
MsgBox("myDict don't contains the list of String, as Key Value")
End If
Try
myDict.Add(newList, actions.Eating)
Catch ex As Exception
MsgBox("myDict don't allow me to add an already present List (Of String), as Key Value")
End Try
End Sub
Dim myDict As New Dictionary(Of List(Of String), actions)(New ListComparer)
End Class
Enum actions
Sleeping
Eating
Studying
Playing
End Enum
NotInheritable Class ListComparer
Inherits EqualityComparer(Of List(Of String))
Public Overrides Function Equals(ByVal x As List(Of String), ByVal y As List(Of String)) As Boolean
Return StructuralComparisons.StructuralEqualityComparer.Equals(x, y)
End Function
Public Overrides Function GetHashCode(ByVal x As List(Of String)) As Integer
Return StructuralComparisons.StructuralEqualityComparer.GetHashCode(x)
End Function
End Class
推荐答案
使用
using System.Collections;
using System.Collections.Generic;
您可以创建类似的类
sealed class ListComparer : EqualityComparer<List<string>>
{
public override bool Equals(List<string> x, List<string> y)
=> StructuralComparisons.StructuralEqualityComparer.Equals(x, y);
public override int GetHashCode(List<string> x)
=> StructuralComparisons.StructuralEqualityComparer.GetHashCode(x);
}
,然后将其用作Dictionary<,>
myDict = new Dictionary<List<string>, Actions>(new ListComparer());
ListComparer
类也可以是通用的sealed class ListComparer<T> : EqualityComparer<List<T>>
.
评论后,我意识到StructuralEqualityComparer
对List<string>
不起作用(我曾想过)!它适用于string[]
和Tuple<string, string, ...>
之类的东西.因此,上述类需要更改为:
After comments, I realize that StructuralEqualityComparer
does not work for List<string>
(which I had thought)! It works for string[]
and for stuff like Tuple<string, string, ...>
. So the above class needs to be changed into:
sealed class ListComparer : EqualityComparer<List<string>>
{
public override bool Equals(List<string> x, List<string> y)
=> StructuralComparisons.StructuralEqualityComparer.Equals(x?.ToArray(), y?.ToArray());
public override int GetHashCode(List<string> x)
=> StructuralComparisons.StructuralEqualityComparer.GetHashCode(x?.ToArray());
}
我最初的尝试是错误的!
My original attempt was wrong!
这篇关于创建一个以键为列表的字典(字符串)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!