I have a problem....I want to know what is the data structure that can be used to store non-unique key -value pairs.....espec ially non-unique keys.....HashMa p cannot be used, right....since it holds only unique keys...pls help
Data Strucutre for non-unique key-value pairs
Collapse
X
-
Originally posted by jyohereI have a problem....I want to know what is the data structure that can be used to store non-unique key -value pairs.....espec ially non-unique keys.....HashMa p cannot be used, right....since it holds only unique keys...pls help
your Map is actually a List or Set holding all the values associated with a key.
kind regards,
Jos -
Originally posted by jyohereI have a problem....I want to know what is the data structure that can be used to store non-unique key -value pairs.....espec ially non-unique keys.....HashMa p cannot be used, right....since it holds only unique keys...pls help
From the docs
Originally posted by thedocsA map cannot contain duplicate keys; each key can map to at most one value.Comment
-
Well of course you could hack it and use a Map as Jos suggested. Simply encapsulate your multiple values into a single Object of some kind (a List, Set, Array, etc.) and store that as your value. Makes your life a bit more difficult, but you could get it to work easily enough. To be super-clear, your mappings would take the form:
<key, List(value 1, value 2, ..., value n)>
Making your own data structure is only worth it if:
a) this is a major and reusable component in your application
b) you have a good algorithm + data structure in mind that will perform better than the above "hack"Comment
-
Originally posted by beatTheDevilWell of course you could hack it and use a Map as Jos suggested. Simply encapsulate your multiple values into a single Object of some kind (a List, Set, Array, etc.) and store that as your value. Makes your life a bit more difficult, but you could get it to work easily enough. To be super-clear, your mappings would take the form:
<key, List(value 1, value 2, ..., value n)>
Making your own data structure is only worth it if:
a) this is a major and reusable component in your application
b) you have a good algorithm + data structure in mind that will perform better than the above "hack"Comment
-
Originally posted by sendit34can you put the impemantation code of this senerio here?
Thanks,
has been given to you already also. What do you want that code for? Hint: it's
a Map taking a type K key and a type List<V> for the values; each value is of
type V.
kind regards,
JosComment
Comment