首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >获取泛型字典的指定值的多个键?

获取泛型字典的指定值的多个键?
EN

Stack Overflow用户
提问于 2008-11-01 00:49:08
回答 11查看 125.2K关注 0票数 126

从.NET通用字典中很容易获得键的值:

代码语言:javascript
复制
Dictionary<int, string> greek = new Dictionary<int, string>();
greek.Add(1, "Alpha");
greek.Add(2, "Beta");
string secondGreek = greek[2];  // Beta

但是,尝试为键指定一个值并不简单,因为可能有多个键:

代码语言:javascript
复制
int[] betaKeys = greek.WhatDoIPutHere("Beta");  // expecting single 2
EN

回答 11

Stack Overflow用户

回答已采纳

发布于 2008-11-01 08:03:09

好的,这是多个双向版本:

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Text;

class BiDictionary<TFirst, TSecond>
{
    IDictionary<TFirst, IList<TSecond>> firstToSecond = new Dictionary<TFirst, IList<TSecond>>();
    IDictionary<TSecond, IList<TFirst>> secondToFirst = new Dictionary<TSecond, IList<TFirst>>();

    private static IList<TFirst> EmptyFirstList = new TFirst[0];
    private static IList<TSecond> EmptySecondList = new TSecond[0];

    public void Add(TFirst first, TSecond second)
    {
        IList<TFirst> firsts;
        IList<TSecond> seconds;
        if (!firstToSecond.TryGetValue(first, out seconds))
        {
            seconds = new List<TSecond>();
            firstToSecond[first] = seconds;
        }
        if (!secondToFirst.TryGetValue(second, out firsts))
        {
            firsts = new List<TFirst>();
            secondToFirst[second] = firsts;
        }
        seconds.Add(second);
        firsts.Add(first);
    }

    // Note potential ambiguity using indexers (e.g. mapping from int to int)
    // Hence the methods as well...
    public IList<TSecond> this[TFirst first]
    {
        get { return GetByFirst(first); }
    }

    public IList<TFirst> this[TSecond second]
    {
        get { return GetBySecond(second); }
    }

    public IList<TSecond> GetByFirst(TFirst first)
    {
        IList<TSecond> list;
        if (!firstToSecond.TryGetValue(first, out list))
        {
            return EmptySecondList;
        }
        return new List<TSecond>(list); // Create a copy for sanity
    }

    public IList<TFirst> GetBySecond(TSecond second)
    {
        IList<TFirst> list;
        if (!secondToFirst.TryGetValue(second, out list))
        {
            return EmptyFirstList;
        }
        return new List<TFirst>(list); // Create a copy for sanity
    }
}

class Test
{
    static void Main()
    {
        BiDictionary<int, string> greek = new BiDictionary<int, string>();
        greek.Add(1, "Alpha");
        greek.Add(2, "Beta");
        greek.Add(5, "Beta");
        ShowEntries(greek, "Alpha");
        ShowEntries(greek, "Beta");
        ShowEntries(greek, "Gamma");
    }

    static void ShowEntries(BiDictionary<int, string> dict, string key)
    {
        IList<int> values = dict[key];
        StringBuilder builder = new StringBuilder();
        foreach (int value in values)
        {
            if (builder.Length != 0)
            {
                builder.Append(", ");
            }
            builder.Append(value);
        }
        Console.WriteLine("{0}: [{1}]", key, builder);
    }
}
票数 150
EN

Stack Overflow用户

发布于 2008-11-01 01:03:00

字典实际上并不是这样工作的,因为键的唯一性是有保证的,值的唯一性是不能保证的。

代码语言:javascript
复制
var greek = new Dictionary<int, string> { { 1, "Alpha" }, { 2, "Alpha" } };

您希望从greek.WhatDoIPutHere("Alpha")中获得什么

因此,你不能期望像这样的东西被加入到框架中。你需要你自己的方法来实现你自己的独特用途-你想返回一个数组(或IEnumerable<T>)吗?如果有多个键具有给定值,是否要抛出异常?如果没有呢?

就我个人而言,我会选择一个可枚举的,如下所示:

代码语言:javascript
复制
IEnumerable<TKey> KeysFromValue<TKey, TValue>(this Dictionary<TKey, TValue> dict, TValue val)
{
    if (dict == null)
    {
        throw new ArgumentNullException("dict");
    }
    return dict.Keys.Where(k => dict[k] == val);
}

var keys = greek.KeysFromValue("Beta");
int exceptionIfNotExactlyOne = greek.KeysFromValue("Beta").Single();
票数 26
EN

Stack Overflow用户

发布于 2008-11-01 01:03:34

也许最简单的方法是在没有Linq的情况下循环遍历这些对:

代码语言:javascript
复制
int betaKey; 
foreach (KeyValuePair<int, string> pair in lookup)
{
    if (pair.Value == value)
    {
        betaKey = pair.Key; // Found
        break;
    }
}
betaKey = -1; // Not found

如果你有Linq,它可以很容易地这样做:

代码语言:javascript
复制
int betaKey = greek.SingleOrDefault(x => x.Value == "Beta").Key;
票数 23
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/255341

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档