Is there a non-indexed dictionary?

I’d like to have a dictionary like:

Dictionary<T, U>

The problem is that a Dictionary does not allow the same indexes twice.
I know from .NET that there is:

Tuple<T, U>

Which is basically a dictionary that allows the same indexes more than once.
But I cannot use it in Unity I believe.
Does anyone know an alternative?

First of all, a dictionary or any other hashtable functions using key-value pairs. The reason key, being known as key are meant to be unique. I believe you are looking for a container for value-value pair instead. For such containers, there is no point of using the values as indices as you would not know which pair you are trying to reference to.

In short, what is the point of having same indices more than once when you don’t know what you are retrieving using a particular index value?

If you really need a container that does not require unique references, just use an ArrayList to store the values and iterate through them.

There is a MultiValueDictionary in the works. But why not just use a dictionary of a lists instead:

Dictionary<Y, List<U>>
2 Likes

ArrayList is a very good point, as well as the Dictionary-List, thanks!
And there are cases when you want to have value-value pairs, otherwise .NET wouldn’t have introduced the Tuple class.
As an example:

private Tuple<KeyTypes KeyType, IKeyListener Listener> _keylisteners = new Tuple<KeyTypes KeyType, IKeyListener Listener>();

private void OnKeyDown(KeyTypes KeyType){
   foreach(Tuple<KeyTypes KeyType, IKeyListener Listener> keyListener in _keyListener){
      if(keyListener.Item1 == KeyType){
         if(keyListener.Item2 == null){
            _keylisteners.Remove(keyListener);
         }else{
            keyListener.Item2.OnKeyOccured(KeyType);
         }
      }
   }
}

Yup but still what you want to do is not to retrieve a particular reference based off a key but to search the container for matching values. In such a case, you should not be looking at Dictionary or Hashtable

Tuple is not a dictionary at all. It doesn’t have indexes. It is just a generic data container. If you have to store multiple elements that could be the same, you could either have a List of Tuples or a Tuple with Lists.

2 Likes