创建一个以键为列表的字典(字符串)

时间:2019-02-02 07:52:52

标签: c# vb.net dictionary

需要创建一个字典,其中的键是列表(字符串)或字符串数​​组

我找到了此链接 C# List as Dictionary key

但是我需要帮助来了解如何使用列表(字符串)

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

我希望包含键的字典输出。

P.S。由于c#与vb.net十分接近,并且在网上有很多c#/ vb.net转换器可以轻松翻译,因此,也欢迎c#帮助。


UPDATE(在Jeppe Stig Nielsen帮助之后,我尝试实现一个继承EqualityComparer的类,但是它不起作用...也许我在语法上犯了错误...有人知道我的方法有什么问题吗?


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

2 个答案:

答案 0 :(得分:2)

使用

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, ...>之类的东西。因此,上述类需要更改为:

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());
}

我最初的尝试是错误的!

答案 1 :(得分:0)

问题是您比较2个对象。您需要比较其中的值。 我不知道vb.net我的代码是c#(LINQ)。这样做。

var myDic = new Dictionary<List<string>, Actions>();
myDic.Add(new List<string>() { "Huey", "Dewey" }, Actions.Sleeping);
myDic.Add(new List<string>() { "Dewey", "Louie" }, Actions.Playing);
var newList = new List<string>() { "Dewey", "Louie" };
if (myDic.Keys.Any(key =>
{
    if (key.Count != newList.Count) return false;

    var same = true;
    for (int i = 0; i < newList.Count; i++)
    {
        if (key[i] != newList[i]) same = false;
    }
    return same;
}))
    MsgBox("myDict contains the list of String, as Key Value");
else
    MsgBox("myDict don't contains the list of String, as Key Value")