获得一个通用的字典价值的关键? [英] Getting key of value of a generic Dictionary?

查看:123
本文介绍了获得一个通用的字典价值的关键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以很容易地获得从.NET 2.0通用词典一键的值:

 词典< INT,字符串>希腊=新字典< INT,字符串>();
greek.Add(1,阿尔法);
greek.Add(2Beta版);
字符串secondGreek =希腊语[2]; // Beta版
 

但有一个简单的方法来获取值的关键?

  INT [] betaKeys = greek.WhatDoIPutHere(Beta版); //期待单2
 

解决方案

好了,这里的多个双向版本:

 使用系统;
使用System.Collections.Generic;
使用System.Text;

类BiDictionary< TFirst,TSecond>
{
    IDictionary的< TFirst,IList的< TSecond>> firstToSecond =新字典< TFirst,IList的< TSecond>>();
    IDictionary的< TSecond,IList的< TFirst>> secondToFirst =新字典< TSecond,IList的< TFirst>>();

    私有静态的IList< TFirst> EmptyFirstList =新TFirst [0];
    私有静态的IList< TSecond> EmptySecondList =新TSecond [0];

    公共无效添加(TFirst第一,TSecond秒)
    {
        IList的< TFirst>首创;
        IList的< TSecond>秒;
        如果(!firstToSecond.TryGetValue(第一,走出秒))
        {
            秒=新的名单,其中,TSecond>();
            firstToSecond [首页] =秒;
        }
        如果(!secondToFirst.TryGetValue(第二,出创举))
        {
            首创=新的名单,其中,TFirst>();
            secondToFirst [二] =首创;
        }
        seconds.Add(第二);
        firsts.Add(第一);
    }

    //注意潜在的不确定性使用索引(例如映射从int到int)
    //因此,方法以及...
    公众的IList< TSecond>这[TFirst首页]
    {
        {返回GetByFirst(第一); }
    }

    公众的IList< TFirst>这[TSecond秒]
    {
        {返回GetBySecond(第二); }
    }

    公众的IList< TSecond> GetByFirst(TFirst第一)
    {
        IList的< TSecond>清单;
        如果(!firstToSecond.TryGetValue(第一,出名单))
        {
            返回EmptySecondList;
        }
        返回新的List< TSecond>(名单); //创建理智副本
    }

    公众的IList< TFirst> GetBySecond(TSecond秒)
    {
        IList的< TFirst>清单;
        如果(!secondToFirst.TryGetValue(第二,出名单))
        {
            返回EmptyFirstList;
        }
        返回新的List< TFirst>(名单); //创建理智副本
    }
}

类测试
{
    静态无效的主要()
    {
        BiDictionary< INT,字符串>希腊=新BiDictionary< INT,字符串>();
        greek.Add(1,阿尔法);
        greek.Add(2Beta版);
        greek.Add(5,贝塔);
        ShowEntries(希腊语,阿尔法);
        ShowEntries(希腊语,贝塔);
        ShowEntries(希腊语,伽玛);
    }

    静态无效ShowEntries(BiDictionary< INT,字符串>字典,字符串键)
    {
        IList的< INT>值=字典[关键];
        StringBuilder的建设者=新的StringBuilder();
        的foreach(中值int值)
        {
            如果(builder.Length!= 0)
            {
                builder.Append(,);
            }
            builder.Append(值);
        }
        Console.WriteLine({0}:[{1}]键,建设者);
    }
}
 

It's easy to get the value of a key from a .Net 2.0 generic Dictionary:

Dictionary<int, string> greek = new Dictionary<int, string>();
greek.Add(1, "Alpha");
greek.Add(2, "Beta");
string secondGreek = greek[2];  // Beta

But is there a simple way to get the key of a value?

int[] betaKeys = greek.WhatDoIPutHere("Beta");  // expecting single 2

解决方案

Okay, here's the multiple bidirectional version:

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

这篇关于获得一个通用的字典价值的关键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆