Click here to Skip to main content
15,886,774 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
So I have a dictionary but it's a bit complex (i.e Dictionary < string, Dictionary < string , Hashset < string > > . How can I print something as complex as this?
Posted
Updated 26-Dec-15 9:55am
v2
Comments
BillWoodruff 26-Dec-15 1:31am    
It's important you clarify what you mean by "print:" print in what form ? Also, one look at your definition of the data-structure tells me it won't compile as-is: please at least make sure it compiles before expecting help with it.
Member 12160712 26-Dec-15 15:52pm    
It did compile! But I actually updated it to a Dictionary < string, Dictionary < string, hashset < string> > >; as I didn't need the hashset I don't know why I did it in the first place. And this does compile too. If you could help me print the updated version of it that would be great.

Context: Andreas (above) asked the OP about the "exotic" nature of his proposed data-structure. The OP replied: "I know it's really bad, and I'm trying to abstract it a bit because it is really hard to work with."

As best as I can understand it, the OP is using this data-structure:
C#
Dictionary<
    string,
    HashSet<
        Dictionary<
            string,
            HashSet<string>>>>
I think it's useful to try and approach improving a data-structure like this by considering how it would "map" to Classes:

I'd start by defining the nested data-elements from "outermost to innermost:"
C#
// outer Dictionary<string,>
public class OuterD : Dictionary<string, InnerH>
{
    public static StringBuilder sb = new StringBuilder();

    public override string ToString()
    {
        sb.Clear();
        sb.AppendLine("Outer Dictionary:");
        
        foreach (var kvp in this)
        {
            sb.Append("\tKey: ");
            sb.AppendLine(kvp.Key);
            sb.Append("\tValue: ");
            sb.Append(kvp.Value.ToString());
            sb.AppendLine();
        }

        return sb.ToString();
    }
}

// the HashSet which is the Value of the outermost Dictionary
public class InnerH : HashSet<InnerD>
{
    public override string ToString()
    {
        // left for you to write: use the StringBuilder like this:
        OuterD.sb.AppendLine("\t\tHashSet of Outer Dictionary Value Value:");

        foreach (var hshval in this)
        {
            // left for you to write
        }

        return "";
    }
}

// the innermost Dictionary used as elements of the HashSet
public class InnerD : Dictionary<string,HashSet<string>>
{
    public override string ToString()
    {
        // left for you to write: use the StringBuilder like this:
        OuterD.sb.AppendLine("\t\t\tInner Dictionary of Inner HashSet:");

        foreach (var kvp in this)
        {
            OuterD.sb.Append("\t\t\tKey: ");
            OuterD.sb.AppendLine(kvp.Key);
            
            foreach (var hsh in kvp.Value)
            {
                // for you to write
            }
        }

        return "";
    } 
}
What "jumps out" at me looking at the data-structure from this "point-of-view" is that you've got two "levels" of hashing going on here: why do you need two; why can't you just use a Dictionary<string, string> for the innermost Dictionary, and only Hash the Dictionary itself ?

I hope you can see who the task of printing ... if by "printing" you mean rendering the current state of the data-structure into one string ... can be done by overloading 'ToString() in each of the Classes and then essentially "chaining" their calls, while providing a 'static StringBuilder to use to hold the assembling string.
 
Share this answer
 
v5
Comments
Member 12160712 26-Dec-15 15:54pm    
Thank you, this is very helpful. Does my updated version need abstraction too? Like I think why I'm not keen on abstraction is cuz I'm reading my data from a text file and I'm not really experienced enough with dealing with text files, so it gets confusing a bit when I think about it with abstraction.
BillWoodruff 27-Dec-15 0:40am    
I'm glad you found my reply helpful, and I think, if you choose to "map" your (revised) data structure into classes, you can easily re-use the code shown here.

re: the issue of working with Text files: if you have the code that creates them, you should be using .NET serialization to save/restore them ... that's the big "win" !
Member 12160712 27-Dec-15 11:29am    
Thank you! I will look up more on .NET serialization.
Andreas Gieriet 28-Dec-15 11:18am    
My 5 for your detailed answer!
Cheers
Andi
BillWoodruff 29-Dec-15 2:09am    
thanks, Andi, and ... Happy New Year !
What you have is a multi-dimensional data structure.
Create a tabular dump, each column represents one dimension.
In your case (assuming your data structure is in variable data):
C#
foreach(var dataKey in data.Keys)
{
    var dataValue = data[dataKey]; // HashSet
    foreach(var dataValueValue in dataValue) // Dictionary
    {
        foreach(var dataValueValueKey in dataValueValue.Keys)
        {
            var dataValueValueValue = dataValueValue[dataValueValueKey]; // HashSet
            foreach(var dataValueValueValueValue in dataValueValueValue) // string
            {
                 Console.WriteLine("{0};{1};{2};{3};{4}"
                                  , dataKey
                                  , dataValue.GetHashCode()
                                  , dataValueValueKey
                                  , dataValueValueValue.GetHashCode()
                                  , dataValueValueValueValue
                                  );
            }
        }
    }
}
Open it in some CSV viewer, e.g. MS Excel.
Cheers
Andi
PS: Instead of the awkward data...Value/data...Key variables give them some logical names.
 
Share this answer
 
v2
Comments
Member 12160712 25-Dec-15 18:10pm    
I'm sorry but I have a question, what do you mean by the gethashkey function? I am not familiar with it in the c# language.
Andreas Gieriet 25-Dec-15 18:33pm    
See my updated solution. My fault - I gave the wrong method name.
Where do you have such a weird data structure from. This is a code smell. You should create reasonable classes which abstract the data structures. In my opinion, such nested data structures are very bad design.
Cheers
Andi
Member 12160712 25-Dec-15 18:39pm    
Thank you! I know it's really bad and I'm trying to abstract it a bit because it is really hard to work with, I'm trying to construct a graph that's read from a text file and this is how I was asked to visualize it but yeah I'm working on abstracting it, Thanks!
BillWoodruff 26-Dec-15 1:32am    
+5 even though we don't know as much as we'd like to know here about the OP's code, and why they've chosen such an exotic data-structure ... I think this is a very helpful answer.
Andreas Gieriet 28-Dec-15 11:18am    
Thanks for your 5!
I agree with your concerns.
Cheers
Andi

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900