0

I want to do a dictionary which contains another dictionary. And I want that my dictionary have multiple values for only one key but I get a error : "An element with the same key already exists". I already did it in JAVA with HashMap and it's ok but in C# ...

Something like this:

static Dictionary<int, Dictionary<double, double>> _dict = new Dictionary<int, Dictionary<double, double>>()
        {
            { 1, new Dictionary<double,double>{ { 0.990, 0.0316 } } },
            { 1, new Dictionary<double,double>{ { 0.975, 0.0398 } } },
            { 1, new Dictionary<double,double>{ { 0.950, 0.0239 } } },
            { 1, new Dictionary<double,double>{ { 0.9  , 0.0158 } } },
            { 1, new Dictionary<double,double>{ { 0.1  , 2.71 } } },
            { 1, new Dictionary<double,double>{ { 0.050, 3.84 } } },
            { 1, new Dictionary<double,double>{ { 0.025, 5.02 } } },
            { 1, new Dictionary<double,double>{ { 0.010, 6.63 } } },

            { 2, new Dictionary<double,double>{ { 0.990, 0.02 } } },
            { 2, new Dictionary<double,double>{ { 0.975, 0.05 } } },
            { 2, new Dictionary<double,double>{ { 0.950, 0.10 } } },
            { 2, new Dictionary<double,double>{ { 0.9  , 0.21 } } },
            { 2, new Dictionary<double,double>{ { 0.1  , 4.60 } } },
            { 2, new Dictionary<double,double>{ { 0.050, 5.99 } } },
            { 2, new Dictionary<double,double>{ { 0.025, 7.38 } } },
            { 2, new Dictionary<double,double>{ { 0.010, 9.21 } } },

            { 3, new Dictionary<double,double>{ { 0.990, 0.12 } } },
            { 3, new Dictionary<double,double>{ { 0.975, 0.22 } } },
            { 3, new Dictionary<double,double>{ { 0.950, 0.35 } } },
            { 3, new Dictionary<double,double>{ { 0.9  , 0.58 } } },
            { 3, new Dictionary<double,double>{ { 0.1  , 6.25 } } },
            { 3, new Dictionary<double,double>{ { 0.050, 7.81 } } },
            { 3, new Dictionary<double,double>{ { 0.025, 9.35 } } },
            { 3, new Dictionary<double,double>{ { 0.010, 11.34 } } },
};

I try the solution of LordTakkera.

I do that because I have to represent KHI2 Table. I do this in JAVA and my soft works perfectly but I don't know how to use REF in .NET.

Cœur
  • 37,241
  • 25
  • 195
  • 267
trainor
  • 21
  • 2

2 Answers2

0

Where you have duplicate keys, like below, you want all these values in a single inner dictionary.

{ 1, new Dictionary<double,double>{ { 0.990, 0.0316 } } },
{ 1, new Dictionary<double,double>{ { 0.975, 0.0398 } } },
{ 1, new Dictionary<double,double>{ { 0.950, 0.0239 } } },
{ 1, new Dictionary<double,double>{ { 0.9  , 0.0158 } } },
{ 1, new Dictionary<double,double>{ { 0.1  , 2.71 } } },
{ 1, new Dictionary<double,double>{ { 0.050, 3.84 } } },
{ 1, new Dictionary<double,double>{ { 0.025, 5.02 } } },
{ 1, new Dictionary<double,double>{ { 0.010, 6.63 } } },

becomes:

{ 1, new Dictionary<double,double>{ 
    { 0.990, 0.0316 }, { 0.975, 0.0398 }, { 0.975, 0.0398     
    }, ........ }}
Simon
  • 2,840
  • 2
  • 18
  • 26
0

As your error suggests, you cannot have multiple values with the same key in a C# dictionary. This is good, what value would be returned by a duplicate key? It would be undefined behavior at best.

However, the code you are writing would work fine with one tweak:

static Dictionary<int, List<Dictionary<double, double>> = new Dictonary<int, List<Dictionary<double, double>>()
{
    {1, new List<Dictionary<double, double>()
        {
            new Dictionary<double,double>{ { 0.990, 0.0316 } },
            new Dictionary<double,double>{ { 0.975, 0.0398 } }
        }
    },
    ...
}

Keys can map to collections (which can hold collections themselves). From the code you posted, this looks like the most likely solution. Let me know if I can clarify anything!

UPDATE: If you are guaranteed that each of these "nested" dictionaries only has one element, you could use the other answer, or a KeyValuePair:

static Dictionary<int, List<KeyValuePair<double, double>> = new Dictonary<int, List<KeyValuePair<double, double>>()
{
    {1, new List<KeyValuePair<double, double>()
        {
            { 0.990, 0.0316 },
            { 0.975, 0.0398 }
        }
    },
    ...
}
BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117