How can I have a key-value data structure that allows duplicate keys?
Is that even possible, if so how?
Should I just be avoiding this to begin with, if so how?
9 Replies
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
A key-value list with duplicate keys does not make sense to me. If you need it, just make a list. What advantages do you see in a key value list if you have duplicates?
Anything wrong with a list dictionary of lists? Also see linq's GroupBy
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
Json :when:
Although not sure if duplicate keys are legal there tbh
Also SortedList allows this
You could use
List<KeyValuePair<TKey, TValue>>
as wellBut why would you, when you can use a tuple?
A tuple wouldn't implicitly cast to a kvp, so it's just for generic code to work with regular dictionaries as well as this. I see no other benefit.
Can you explain your use case a little more?
you could do a custom datatype thast a
Dictionary<TKey, List<TValue>>
under the hood. when you add a key value pair it appends the value to the list.
thats close to what you want i think without knowing what your usecase is