Size of Dictionary Object

  • Thread starter Thread starter shrishjain
  • Start date Start date
S

shrishjain

Hi All,

I have to make multiple dictionary objects(Dictionary<string key, int
value>) with same set of string keys, and that really eats up lot of
space. I am trying to find a solution where I can save space- In my
current implementation all dictionary objects store the same keys
repetitively and that eats up lot of space. Does .net framework comes
to help in such situation?

The best I can think of is having a separate dictionary object which
maps given set of string keys to set of unique integers, and then use
that unique integers to map to value - this way I don;t have to store
string keys multiple times. Can someone suggest me a better solution.

Thanks,
Shrish
 
Shrish,

I'm not exactly sure what you're trying to accomplish, but is each
Dictionary storing integers for the values (but using the same keys)?

If so, you could use an int[] as the value. If not, maybe you can use
another type of array or collection.

Also, if you need some sort of identifier for the values you are
storing, you could use Dictionary<string, Dictionary<string, int>> which
would give you just 1 key, and then a dictionary for that key with
strings and integers.

Hope this helps.

Dan Manges
 
Why not just use a structure as the value, which has fields which store
the possible values.

For example, if one Dictionary is a Dictionary<string, MyObject> and
another is Dictionary<string, MyObject2>, then you could do this:

public struct MyObjects
{
public MyObject MyObject;
public MyObject2 MyObject2;
}

And then use a Dictionary<string, MyObjects>, setting the appropriate
fields, and using only one set of keys.

Hope this helps.
 
Would this kind of approach help:

//(using System.Collections)

SortedList sortedList = new SortedList();
ArrayList arrayList1 = new ArrayList();
ArrayList arrayList2 = new ArrayList();

arrayList1.Add(1);
arrayList1.Add(3);
arrayList1.Add(5);

arrayList2.Add(0);
arrayList2.Add(2);
arrayList2.Add(4);

string strKey1 = "int collection one";
string strKey2 = "int collection two";

sortedList.Add(strKey1, arrayList1);
sortedList.Add(strKey2, arrayList2);

IDictionaryEnumerator oIDictionaryEnumerator =
sortedList.GetEnumerator();

while (oIDictionaryEnumerator.MoveNext())
{
Console.WriteLine("key = {0}",
oIDictionaryEnumerator.Key);

ArrayList arrayList = oIDictionaryEnumerator.Value as
ArrayList;
IEnumerator oIEnumerator = arrayList.GetEnumerator();
while (oIEnumerator.MoveNext())
{
Console.WriteLine("value = {0}",
oIEnumerator.Current);
}
}
 
Back
Top