I have a data like in (string , int) pair. How to store this data in collection object. Both values can be duplicate. Which collection object should i use??
EDIT: How can i access elements separately..??
[C#] Dictionary with duplicate keysThe Key value of a Dictionary is unique and doesn't let you add a duplicate key entry. To accomplish the need of duplicates keys, i used a List of type KeyValuePair<> .
If you're adding pairs to a List , you can even hold duplicates of pair entries.
In Dictionary, the key cannot be null, but value can be. In Dictionary, key must be unique. Duplicate keys are not allowed if you try to use duplicate key then compiler will throw an exception. In Dictionary, you can only store same types of elements.
You cannot do it by Java collection. You can use Multimap it supports duplicate keys but it also support duplicate keys and value pairs.
You can use List<KeyValuePair<string,int>>
.
This will store a list of KeyValuePair
's that can be duplicate.
You can use List<KeyValuePair<string, int>>
if you want to add & remove items, or KeyValuePair<string, int>[]
if the number of items is known
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With