This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Key Value pairs help

 
Murad Iqbal
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Could someone pelase suggest which data structure should I use? I have a number of key value pairs where duplicates are allowed. Which data structure should I use?

Murad.
 
Darryl Failla
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If duplicate keys are allowed, it's not a key. You're never going to be able to find anything even if you had a collection. If your true requirements provide for "keys" with multiple values, you'll have to iterate through the whole structure anyway to find all possible values.

If that's the case and you know from the start the maximum number pairs you will have, see if use can a two-dimensional array.

Another option is to incorporate your "key" and value into a new Object type. Then make that the value to a true key signature.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another option might be to have a Collection as the value associated with a key. Then when you have another key value pair with the same key, you could add that value to the Collection associated with the key.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic