比较字典< string,List< object>> [英] Compare Dictionary<string,List<object>>

查看:191
本文介绍了比较字典< string,List< object>>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在比较两个字典(dic1和dic2)与规则,从dic2获取值,其中键匹配但值不匹配,或者在dic2中缺少键。

不需要遍历dic2在dic1中缺少/不同的值。



以下代码正常工作我想知道有没有更好的方法使用.NET 2.0(NO LINQ)。



如果优化需要哪个选项更好?

 字典< string,List< ; foo>>> dic1 = new Dictionary< string,List< foo>>(); 
字典< string,List< foo>> dic2 = new Dictionary< string,List< foo>>();

dic1.add(1,new foo(a));
dic1.add(2,new foo(b));
dic1.add(3,new foo(c));
dic1.add(3,new foo(c1));
dic1.add(4,new foo(d));

dic2.add(1,new foo(a));
dic2.add(2,new foo(b1));
dic2.add(3,new foo(c));
dic2.add(3,new foo(c2));

//我编写的代码允许重复键字典

选项1

  foreach(KeyValuePair< string,List< foo>> dic1中的var)
{
if (dic2.ContainsKey(var.Key))
{
列表< foo> tempList = var.Value.FindAll(delegate(foo s)
{
return!dic2 [var.Key] .Contains(s);
});
result.AddRange(tempList);
}
else
{
result.Add(var.Value);
}

}

选项2

 列表< string> list1key = new List< string>(dic1.Keys); 

list1key.ForEach(delegate(string key)
{
if(dic2.ContainsKey(key))
{
列表< foo> tempList = dic1 [key] .FindAll(delegate(foos)
{
return!dic2 [key] .Contains(s);
});
result.AddRange(tempList);
}
else
{
result.AddRange(dic1 [key]);
}
});


解决方案

如果您使用TryGetValue访问dic2时,所以只需要一次查找一次。



您的第一个选项看起来更简单,可能更快,我会去。
干杯


I am comparing two dictionary(dic1 and dic2) with rule that get values from dic2 where key match but values does not match or key is missing in dic2.
Don’t need to iterate through dic2 for missing/different values in dic1.

Below code is working ok I would like to know is there any better way using .NET 2.0 (NO LINQ) .

if optimization is require which option is better?

Dictionary<string,List<foo>> dic1 = new Dictionary<string,List<foo>>();
Dictionary<string,List<foo>> dic2 = new Dictionary<string,List<foo>>();

dic1.add("1", new foo("a"));
dic1.add("2", new foo("b"));
dic1.add("3", new foo("c"));
dic1.add("3", new foo("c1"));
dic1.add("4", new foo("d"));

dic2.add("1", new foo("a"));
dic2.add("2", new foo("b1"));
dic2.add("3", new foo("c"));
dic2.add("3", new foo("c2"));

//I write code which allow duplicate key in dictionary 

Option 1

foreach (KeyValuePair<string, List<foo>> var in dic1)
{
    if (dic2.ContainsKey(var.Key))
    {
        List<foo> tempList = var.Value.FindAll(delegate(foo s)
        {
            return !dic2[var.Key].Contains(s);
        });
        result.AddRange(tempList);
    }
    else
    {
        result.Add(var.Value);
    }

}

Option 2

List<string> list1key = new List<string>(dic1.Keys);

list1key.ForEach(delegate(string key)
{
    if (dic2.ContainsKey(key))
    {
        List<foo> tempList = dic1[key].FindAll(delegate(foos)
           {
               return !dic2[key].Contains(s);
           });
        result.AddRange(tempList);
    }
    else
    {
        result.AddRange(dic1[key]);
    }
});

解决方案

You can speed things up with either option if you use TryGetValue when accessing dic2, so you only have to do a key lookup once.

Your first option looks simpler and possibly faster, i'd go with that. Cheers

这篇关于比较字典&lt; string,List&lt; object&gt;&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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