Combination of two dictionarie

P

psykoprogrammer

I have a situation where I have two Dictionary<string, string> objects
and I need to combine them. More the point I need to update the value
of all the keys in dictionary A with values from matching keys in
dictionary B. Any keys in dictionary B that do no exist in dictionary
A are to be inserted into A. To illustrate:

Dictionary A:
Name, Adam
Age, 30

Dictionary B:
Name, Bob
Job, Dishwasher

Resulting Dictionary:
Name, Bob
Age, 30
Job, Dishwasher

What would be the most effecient way to accomplish this in .NET 3.5?
Is Dictionary even the right choice?

Thanks!
 
A

Alberto Poblacion

psykoprogrammer said:
I have a situation where I have two Dictionary<string, string> objects
and I need to combine them. More the point I need to update the value
of all the keys in dictionary A with values from matching keys in
dictionary B. Any keys in dictionary B that do no exist in dictionary
A are to be inserted into A. To illustrate:
[...]
What would be the most effecient way to accomplish this in .NET 3.5?
Is Dictionary even the right choice?

I believe that a simple loop on B would be most efficient, since it performs
a single pass over the elements of B, which would need to be done anyway,
and looks up each of them in Dictionary A, which implements an efficient
lookup algorithm.

You could do something similar to the following (untested, might not even
compile):

foreach (KeyValuePair<string, string> kvp in B)
{
if (A.ContainsKey(kvp.Key))
A[kvp.Key] = kvp.Value;
else
A.Add(kvp.Key, kvp.Value);
}
 
P

psykoprogrammer

Thank you very much. That's kinda what I figured, as the simples
solution is usually best. I was looking at the Join method on the
Dictionary object, but have not quite used it before, and started
getting into LINQ, and that was a bit to swallow at 1am, though quite
interesting.

I have a situation where I have two Dictionary<string, string> objects
and I need to combine them. More the point I need to update the value
of all the keys in dictionary A with values from matching keys in
dictionary B. Any keys in dictionary B that do no exist in dictionary
A are to be inserted into A. To illustrate:
[...]
What would be the most effecient way to accomplish this in .NET 3.5?
Is Dictionary even the right choice?

I believe that a simple loop on B would be most efficient, since it performs
a single pass over the elements of B, which would need to be done anyway,
and looks up each of them in Dictionary A, which implements an efficient
lookup algorithm.

You could do something similar to the following (untested, might not even
compile):

foreach (KeyValuePair<string, string> kvp in B)
{
    if (A.ContainsKey(kvp.Key))
       A[kvp.Key] = kvp.Value;
    else
       A.Add(kvp.Key, kvp.Value);

}
 
A

Alberto Poblacion

Peter Duniho said:
Minor nit: since assigning via the indexer a value for which no key is
already present will add the value to the dictionary, the above code can
be even simpler (and faster):

You always learn something new! I had always thought that assigning via
the indexer to a non-existent key threw an exception.
 
J

Jeff Johnson

You always learn something new! I had always thought that assigning via
the indexer to a non-existent key threw an exception.

I agree that it's handy, but it feels kind of inconsistent.

Attempt to read from a non-existent key: Error!
Attempt to write to a non-existent key: No problem!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top