Dictionary with exact same keys and values

I need something like a dictionary or a SortedList, but I keep getting exceptions when it gets two things that are the same ... Is there any other way?

thanks

+4
source share
5 answers

You probably want a multimap . You can mimic one with Dictionary<Key, List<Value>> . Also see this question , it has several implementations with several modes.

+4
source

I assume that you are using

 dictionary.Add(key, value); 

If you're happy just replacing the existing key / value pair, just use the index:

 dictionary[key] = value; 

If you want to have multiple values ​​for the same key, see other answers :)

+4
source

The key in a dictionary or sorted list, the key must be unique: the target of the hash tables. Instead, you can use List<KeyValuePair<TKey, TValue>> or KeyValuePair<TKey, TValue>[] , but, of course, you cannot find the value by key, because you may have duplicates.

0
source

Both the dictionary and the sorted list are entered by the key, which means that the keys must be unique.

You can check before adding an item, for example. dic.ContainsKey(key) or list.Contains(item) before adding.

If you want to add multiple values ​​for a single key, you can use NameValueCollection .

0
source

If what you are asking for is a dictionary where the value matches the key, then there is a generic collection type named "HashSet" in the System.Collections.Generic namespace, which looks like it serves this purpose.

If you're just asking about the exception that is thrown when added to the dictionary, I think Jon Skeet is probably right that your problem is probably.

Hashset:

http://msdn.microsoft.com/en-us/library/bb359438.aspx

0
source

Source: https://habr.com/ru/post/1310617/


All Articles