.NET Dictionary:获取或创建新的

时间:2013-04-24 13:09:53

标签: c# .net dictionary insert-update

我经常发现自己创建了一个带有非平凡值类的Dictionary(例如List),然后在填写数据时总是编写相同的代码模式。

例如:

var dict = new Dictionary<string, List<string>>();
string key = "foo";
string aValueForKey = "bar";

也就是说,我想将"bar"插入与密钥"foo"对应的列表中,其中密钥"foo"可能不会映射到任何内容。

这是我使用不断重复的模式的地方:

List<string> keyValues;
if (!dict.TryGetValue(key, out keyValues))
  dict.Add(key, keyValues = new List<string>());
keyValues.Add(aValueForKey);

有更优雅的方式吗?

对此问题没有答案的相关问题:

8 个答案:

答案 0 :(得分:38)

我们对此略有不同,但效果类似:

public static TValue GetOrCreate<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey key) 
    where TValue : new()
{
    TValue val;

    if (!dict.TryGetValue(key, out val))
    {
        val = new TValue();
        dict.Add(key, val);
    }

    return val;
}

调用:

var dictionary = new Dictionary<string, List<int>>();

List<int> numbers = dictionary.GetOrCreate("key");

它使用公共无参数构造函数的通用约束:where TValue : new()

为了帮助发现,除非扩展方法非常特定于一个狭窄的问题,我们倾向于将扩展方法放在它们扩展的类型的命名空间中,在这种情况下:

namespace System.Collections.Generic

大多数情况下,使用该类型的人员在顶部定义了using语句,因此IntelliSense也会在您的代码中找到它所定义的扩展方法。

答案 1 :(得分:4)

与许多编程问题一样,当你发现自己做了很多事情时,将它重构为一个方法:

public static void MyAdd<TKey, TCollection, TValue>(
    this Dictionary<TKey, TCollection> dictionary, TKey key, TValue value)
    where TCollection : ICollection<TValue>, new()
{
    TCollection collection;
    if (!dictionary.TryGetValue(key, out collection))
    {
        collection = new TCollection();
        dictionary.Add(key, collection);
    }
    collection.Add(value);
}

答案 2 :(得分:2)

如果构造函数需要参数,这是一种解决方案。

public static TValue GetOrCreate<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey key, Func<TValue> createNew)
    {
        if (!dict.TryGetValue(key, out var val))
        {
            val = createNew();
            dict.Add(key, val);
        }

        return val;
    }

简单易用:

MyDict.GetOrCreate(si.Id, createNew: () => new ObjectKnowingItsId(si.Id))

答案 3 :(得分:2)

对于更多读者来说,这是我认为合适的每种口味的扩展。如果需要检查是否已添加值,也可以使用out参数执行操作,但我认为您可以使用containskey或已经执行的操作。

您可以使用GetOrAddNew检索项目,或创建并将其添加到字典。您可以使用GetOrAdd的各种重载来添加新值。这可能是default,例如NULL0,但您也可以提供lambda来为您构造一个对象,其中包含您想要的任何构造函数参数。

var x = new Dictionary<string, int>();
var val = x.GetOrAdd("MyKey", (dict, key) => dict.Count + 2);
var val2 = x.GetOrAdd("MyKey", () => Convert.ToInt32("2"));
var val3 = x.GetOrAdd("MyKey", 1);
    /// <summary>
    /// Extensions for dealing with <see cref="Dictionary{TKey,TValue}"/>
    /// </summary>
    public static class DictionaryExtensions
    {
        public static TValue GetOrAddNew<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey key, TValue defaultValue = default) 
            where TValue : new() 
            => dict.GetOrAdd(key, (values, innerKey) => EqualityComparer<TValue>.Default.Equals(default(TValue), defaultValue) ? new TValue() : defaultValue);

        public static TValue GetOrAdd<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey key, TValue defaultValue = default)
            => dict.GetOrAdd(key, (values, innerKey) => defaultValue);

        public static TValue GetOrAdd<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey key, Func<TValue> valueProvider)
            => dict.GetOrAdd(key, (values, innerKey) => valueProvider());

        public static TValue GetOrAdd<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey key, Func<TKey, TValue> valueProvider)
            => dict.GetOrAdd(key, (values, innerKey) => valueProvider(key));

        public static TValue GetOrAdd<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey key, Func<IDictionary<TKey, TValue>, TKey, TValue> valueProvider)
        {
            if (dict == null) throw new ArgumentNullException(nameof(dict));
            if (key == null) throw new ArgumentNullException(nameof(key));
            if (valueProvider == null) throw new ArgumentNullException(nameof(valueProvider));

            if (dict.TryGetValue(key, out var foundValue))
                return foundValue;

            dict[key] = valueProvider(dict, key);
            return dict[key];
        }
    }

答案 4 :(得分:0)

好的,不同的方法:

public static bool TryAddValue<TKey,TValue>(this System.Collections.Generic.IDictionary<TKey,List<TValue>> dictionary, TKey key, TValue value)
    {
        // Null check (useful or not, depending on your null checking approach)
        if (value == null)
            return false;

        List<TValue> tempValue = default(List<TValue>);

        try
        {
            if (!dictionary.TryGetValue(key, out tempValue))
            {
                dictionary.Add(key, tempValue = new List<TValue>());
            }
            else
            {
                // Double null check (useful or not, depending on your null checking approach)
                if (tempValue == null)
                {
                    dictionary[key] = (tempValue = new List<TValue>());
                }
            }

            tempValue.Add(value);
            return true;
        }
        catch
        {
            return false;
        }
    }

通过这种方式,您必须“尝试将”值添加到通用List(显然可归于泛型集合),null检查并尝试获取Dictionary中的现有键/值。 用法和示例:

var x = new Dictionary<string,List<string>>();
x.TryAddValue("test", null); // return false due to null value. Doesn't add the key
x.TryAddValue("test", "ok"); // it works adding the key/value
x.TryAddValue("test", "ok again"); // it works adding the value to the existing list

希望它有所帮助。

答案 5 :(得分:0)

那怎么样?

var keyValues = dictionary[key] = dictionary.ContainsKey(key) ? dictionary[key] : new List<string>();
keyValues.Add(aValueForKey);

答案 6 :(得分:0)

如果使用.Net Core,则可以使用Dictionary<>.TryAdd()

var dict = new Dictionary<string, string>();
dict.TryAdd("foo", "bar"); // returns bool whether it added or not feel free to ignore.
var myValue = dict["foo"];

答案 7 :(得分:0)

我缺少Dictionary的{​​{1}}的GetOrAdd。

ConcurrentDictionary

此代码将为每个号码生成向导。最终将ConcurrentDictionary<int,Guid> Conversion = new ConcurrentDictionary<int, Guid>(); List<int> before = new List<int> { 1, 2, 1, 3 }; List<Guid> after = before.Select(x => Conversion.GetOrAdd(x, Guid.NewGuid())).ToList(); 中的两个1都转换为相同的Guid。

在您的情况下:

before
相关问题