How can I shorten List>>?

后端 未结 5 1144
醉酒成梦
醉酒成梦 2021-01-19 07:04

I want to store an list of key value pair lists in a lightweight structure. This seems too cumbersome. What\'s better? Does List> add

5条回答
  •  粉色の甜心
    2021-01-19 07:50

    If you are accessing the values by the key, use dictionary, that is what is meant for. To reduce the overhead of the dictionaries, try and give them an initial capacity.

    Alternatively (if both lists are rather small), you could implement a custom hashing object, something like this (except prettier):

    public class MyDictionary
    {
        private Dictionary values;
    
        public MyDictionary()
        {
            values = new Dictionary();
        }
    
        public void Add(int index, string key, string value)
        {
            int hash = ((0xFFFF) & index) * (0xFFFF) + (0xFFFF) & key.GetHashCode();
            values.Add(hash, value);
        }
        public string Get(int index, string key)
        {
            int hash = ((0xFFFF) & index) * (0xFFFF) + (0xFFFF) & key.GetHashCode();
            return values[hash];
        }
    }
    

提交回复
热议问题