Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

comparing two dictionaries

Status
Not open for further replies.

davikokar

Technical User
May 13, 2004
523
IT
I have two dictionaries<string, int> I Would like to see if the have the same content (equals). I can I do this?
 
You can't do it using Equals if you actually have two dictionary objects rather than two references to the same one. If you really have two dictionaries, and not two references to the same one, you'll probably need to write a method that iterates the dictionaries and compares values along the way, returning false if two unequal values are found.
 
instead of a string, int dictionary create an explicit object to contain the values. override equals and gethashcode and then compare the contents of each collection.
Code:
class MyDto
{
   public string key {get; private set;}
   public int value {get; private set;}
   
   public MyDto(string key, int value)
   {
        this.key = key;
        this.value = value;
   }

   public override int GetHashCode()
   {
       //assumes get will never be null. value has a default value of 0 so nulls are not an issue.
       return key.GetHashCode() ^ 37 + value.GetHashCode();
   }

   public override boll Equals(object other)
   {
         if(ReferenceEquals(null, other)) return false;
         if(ReferenceEquals(this, other)) return true;
         return other.GetType() == typeof(MyDto) 
                && other.key == key 
                && other.value == value;
   }
}
now you can put MyDto into a list<mydto>, instead of a dictionary<string, int> and compare
Code:
var first = get_list_of_mydtos();
var second = get_another_list_of_mydtos();

foreach(var f in first)
{
    if(second.Contains(f)) yield return f;
}

foreach(var s in second)
{
    if(first.Contains(s)) yield return s;
}
there are probably better ways to compare lists. if you using .net 3.5 you may want to look at the Intersect() extension method for IEnumerable<>. If you are using the nhibernate, or the IES.Collections assembly ISet<> also has this functionality.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Yes, you'll have to do something like that. :) Jason, quick question: what's the point of raising the result of key.GetHashCode to a power of 37, if that's in fact what you're doing?
 
uniqueness. it's the default formula provided by resharper when you override equality and hashcode. I use their formula when i manually override getashcode.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thanks to all for the suggestions. In the end I came up with something like that:

Code:
bool isEqual = true;
foreach (KeyValuePair<string, int> kvpExpected in expected)
{
    isEqual = output.Contains(kvpExpected);
}
Assert.IsTrue(isEqual);

output and expected are the two dictionaries.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top