c# - Multi-Keys Hash or Dictionary, a list of values as output -


i new c# , need have general list multiple keys.

i have 3 parameter creates key of data. each record (each 3 key) have set of values.

i need have generic list value of each node in list values of keys , value of each node pointing list contained related values key.

following example of data , data structure looking for:

key1 key2 key3 value1   value2   value3 0     0     0             b      c 0     0     1     d         e      f 0     1     1     g         h      - 
<0,0,0, list(a,b,c)> ---> <0,0,1,list(d,e,f)>---> <0,1,1,list(g,h)>--->null 

i thinking of having hash table multiple keys , value pointing object link list. or creating dictionary these 3 keys , again returning pointer head of link list.

i appreciate if can tell me how can in c#.

first, should use dictionary<tkey, tvalue>, , not hashtable. non-generic collection types there backward compatibility. better use generic types new code.

as specific problem, you'll note .net dictionary types allow single key. indeed, typical dictionary collections generally. each entry in collection single key/value pair.

however, can combine 3 key values single object value , use key instead. , in fact, .net provides various tuple classes accomplish that, different class each count of type parameters, , each count of items in object. furthermore, these classes implement appropriate comparisions , hashing use dictionary key.

now, applying question, have options, depending on want do. unfortunately, it's not clear want do. :(

if ever have maximum of 3 values each triplet of key values, think suggestion commenter mephy fine. declare collection , initialize this:

dictionary<tuple<int, int, int>, tuple<string, string, string>> collection =     new dictionary<tuple<int, int, int>, tuple<string, string, string>>     {         { tuple.create(0, 0, 0), tuple.create("a", "b", "c") },         { tuple.create(0, 0, 1), tuple.create("d", "e", "f") },         { tuple.create(0, 1, 1), tuple.create("g", "h", null) },     }; 

note null used indicate missing value in dictionary's value tuple.

however, if literally want list object value, instead this:

dictionary<tuple<int, int, int>, list<string>> collection =     new dictionary<tuple<int, int, int>, list<string>>     {         { tuple.create(0, 0, 0), new list<string> { "a", "b", "c"} },         { tuple.create(0, 0, 0), new list<string> { "d", "e", "f"} },         { tuple.create(0, 0, 0), new list<string> { "g", "h" } },     }; 

as far treating above list of key/value pairs, .net collection type, dictionary<tkey, tvalue> can treated enumeration of values, in case via implementation of ienumerable<keyvaluepair<tkey, tvalue>> tkey , tvalue same types used dictionary object itself. can example this:

foreach (keyvaluepair<tuple<int, int, int>, list<string>> kvp in collection) {     // here, kvp.key have tuple<int, int, int> key value     // dictionary entry, while kvp.value have     // list<string> value same entry. } 

note order of enumeration of dictionary type in .net undefined. not given assurances elements returned in particular order, e.g. in order in added. if need particular order, you'll have impose somehow.

finally, note in above example keyvaluepair<tkey, tvalue> type. in fact special case of tuple (though pre-dates actual tuple... classes in .net). i.e. it's custom class designed purpose of storing key/value pairs.

you can, if want, declare such type act key dictionary. have advantage of allowing provide specific, readable name type, , of course allowing avoid of verbosity involved in dealing tuple... classes (the tuple.create() generic methods help, declarations can still unwieldy). doing comes of course @ expense of writing own comparison , hash code.

you can find middle ground, either creating class inherits tuple... class need, in implement constructor (passing initialization parameters base constructor), e.g.:

class customkey : tuple<int, int, int> {     public customkey(int i1, int i2, int i3) : base(i1, i2, i3) { } } 

or aliasing tuple... type in module using directive, giving tuple... type locally usable name more readable, e.g.:

using customkey = system.tuple<int, int, int>; 

the former gives easy access readable name anywhere in project require implementing (very short) class; latter requires less work, apply in single source file.


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -