.net字典和查找添加/更新 [英] .net dictionary and lookup add / update

查看:34
本文介绍了.net字典和查找添加/更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我讨厌为我拥有的各种代码编写这样的代码块:

  if(dict.ContainsKey [key]){
dict [key] = value;
}
else {
dict.Add(key,value);
}

并用于查找(即键->值列表)

  if(lookup.ContainsKey [key]){
lookup [key] .Add(value);
}
else {
lookup.Add(new List< valuetype>);;
lookup [key] .Add(value);
}

我是否应该使用另一个collection lib或扩展方法不管键和值类型是什么,代码行都是什么?



例如

  dict.AddOrUpdate(key,value)
查找.AddOrUpdate(key,value)


just 想要无条件地设置给定键的值,则可以执行

  dictionary [key] = value; 

更有趣的情况是获取值,或在必要时将其插入。使用扩展方法很容易:

 公共静态TValue GetOrCreateValue< TKey,TValue> 
(此IDictionary< TKey,TValue>字典,
TKey键,
TValue值)
{
返回字典.GetOrCreateValue(key,()=>值);
}

公共静态TValue GetOrCreateValue< TKey,TValue>
(此IDictionary< TKey,TValue>字典,
TKey键,
Func< TValue> valueProvider))
{
TValue ret;
if(!dictionary.TryGetValue(key,out ret))
{
ret = valueProvider();
dictionary [key] = ret;
}
回返;
}

请注意,使用委托创建默认值-可以简化诸如清单为价值之一;您除非要执行以下操作,否则不想创建空列表:

  dict.GetOrCreateValue(key,()=> new List< int>())。Add(项目); 

还请注意,如果键已经存在,此操作仅执行一次查找-无需这样做 ContainsKey 然后查找值。在创建新值时,仍然需要两次查找。


I am sick of doing blocks of code like this for various bits of code I have:

if (dict.ContainsKey[key]) {  
    dict[key] = value;  
}  
else {  
    dict.Add(key,value);  
}

and for lookups (i.e. key -> list of value)

if (lookup.ContainsKey[key]) {  
    lookup[key].Add(value);  
}  
else {  
    lookup.Add(new List<valuetype>);  
    lookup[key].Add(value);  
}  

Is there another collections lib or extension method I should use to do this in one line of code no matter what the key and value types are?

e.g.

dict.AddOrUpdate(key,value)  
lookup.AddOrUpdate(key,value)

解决方案

As Evgeny says, the indexer will already replace existing values - so if you just want to unconditionally set the value for a given key, you can do

dictionary[key] = value;

The more interesting case is the "get a value, or insert it if necessary". It's easy to do with an extension method:

public static TValue GetOrCreateValue<TKey, TValue>
    (this IDictionary<TKey, TValue> dictionary,
     TKey key,
     TValue value)
{
    return dictionary.GetOrCreateValue(key, () => value);
}

public static TValue GetOrCreateValue<TKey, TValue>
    (this IDictionary<TKey, TValue> dictionary,
     TKey key,
     Func<TValue> valueProvider)
{
    TValue ret;
    if (!dictionary.TryGetValue(key, out ret))
    {
        ret = valueProvider();
        dictionary[key] = ret;
    }
    return ret;
}

Note the use of a delegate to create the default value - that facilitates scenarios like the "list as value" one; you don't want to create the empty list unless you have to:

dict.GetOrCreateValue(key, () => new List<int>()).Add(item);

Also note how this only performs the lookup once if the key is already present - there's no need to do a ContainsKey and then look up the value. It still requires two lookups when it's creating the new value though.

这篇关于.net字典和查找添加/更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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