Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best implementation for Key Value Pair Data Structure?

So I've been poking around with C# a bit lately, and all the Generic Collections have me a little confused. Say I wanted to represent a data structure where the head of a tree was a key value pair, and then there is one optional list of key value pairs below that (but no more levels than these). Would this be suitable?

public class TokenTree {     public TokenTree()     {         /* I must admit to not fully understanding this,          * I got it from msdn. As far as I can tell, IDictionary is an          * interface, and Dictionary is the default implementation of          * that interface, right?          */         SubPairs = new Dictionary<string, string>();     }      public string Key;     public string Value;     public IDictionary<string, string> SubPairs; } 

It's only really a simple shunt for passing around data.

like image 759
Bernard Avatar asked Aug 12 '08 13:08

Bernard


People also ask

Which data structure is used for key-value pair?

key-value store, or key-value database is a simple database that uses an associative array (think of a map or dictionary) as the fundamental data model where each key is associated with one and only one value in a collection. This relationship is referred to as a key-value pair.

How are key-value pairs implemented?

Implement Key Value Pair Using the Map.Entry interface to create a custom class that will hold data in key-value pairs. We create a class, Student , with two instance variables to hold key and value pair. We also created getters and setters methods to set values for each instance of this class. See the example below.


2 Answers

There is an actual Data Type called KeyValuePair, use like this

KeyValuePair<string, string> myKeyValuePair = new KeyValuePair<string,string>("defaultkey", "defaultvalue"); 
like image 138
Adam Haile Avatar answered Oct 02 '22 17:10

Adam Haile


One possible thing you could do is use the Dictionary object straight out of the box and then just extend it with your own modifications:

public class TokenTree : Dictionary<string, string> {     public IDictionary<string, string> SubPairs; } 

This gives you the advantage of not having to enforce the rules of IDictionary for your Key (e.g., key uniqueness, etc).

And yup you got the concept of the constructor right :)

like image 30
Jon Limjap Avatar answered Oct 02 '22 19:10

Jon Limjap