Data Strucutre for non-unique key-value pairs

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jyohere
    New Member
    • Apr 2007
    • 73

    Data Strucutre for non-unique key-value pairs

    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
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by jyohere
    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
    Instead of key-value pairs think of key-values (plural), so the value object in
    your Map is actually a List or Set holding all the values associated with a key.

    kind regards,

    Jos

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by jyohere
      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
      You'll have to make your own type which is not a map.

      From the docs

      Originally posted by thedocs
      A map cannot contain duplicate keys; each key can map to at most one value.

      Comment

      • beatTheDevil
        New Member
        • Nov 2006
        • 16

        #4
        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

        • jyohere
          New Member
          • Apr 2007
          • 73

          #5
          Originally posted by beatTheDevil
          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"
          That was great....it worked thaannnnnnnnnnn x......

          Comment

          • sendit34
            New Member
            • May 2007
            • 1

            #6
            can you put the implemantation code of this senerio here?



            may be something like this ?


            or you have some oyher impl?


            Thanks,

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by sendit34
              can you put the impemantation code of this senerio here?

              Thanks,
              The code doesn't take many more characters than this message; the clue
              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,

              Jos

              Comment

              Working...