What is the best data structure for this in-memory lookup table?

I need to save the lookup table as an instance member in one of my classes. When the object is created, the table will be initialized. Each "row" will have 3 "columns":

StringKey (e.g., "car")
EnumKey (e.g., LookupKeys.Car)
Value (e.g, "Ths is a car.")

I want to choose a data structure that will give the best performance for performing searches using either StringKey or EnumKey.

It's awkward having 2 keys for the same vocabulary value. I have never encountered this before, so I wonder what the norm is for this type of thing.

I can create a Key / Value / Value structure instead of Key / Key / Value, but I'm wondering what type of performance impact it will have.

I think this is all wrong?

+5
source share
5 answers

You have two hashmaps.

  • One of StringKey for value.

  • One of EnumKey for value.

You do not need to duplicate all instances of Value, these objects can be divided between two hash maps.

If you have a lot of items, you can use two treemaps instead of two hash maps. But the basic principle ("Share the Values") applies to both structures. One set of values ​​with two maps.

+4
source

... "" - . , , - " ", , , , .

+5

? , , . - , , . " " .

, .

+1

LINQ ILookup (TKey, TElement) . , - - :

Dictionary<carKey, carValue> cars;

:

ILookUp<carValue, carKey> lookup = cars.ToLookup(x => x.Value, x => x.Key);

(... , , , , , ILookUp - , /, , .)

0

, . :

public Value getValue(String key)
{
    dictionary.get(key); // normal way
}

public Value getValue(Enum enumKey)
{
    String realKey = toKey(enumKey);
    getValue(realKey); // use String key
}

You can force your Enum to implement the toKey () method, which returns its String key, or perhaps another dictionary that maps Enum values ​​to String instances.

0
source

All Articles