Frage

I am using multi key bags in order to count occurrences of certain combinations of values and I was wondering if there is an elegant way to convert these bags into nested SortedMaps like TreeMaps. The number of nested TreeMaps being equal to the number of components in the multi key. For instance, let's say I have a multi key bag which has a defined key:

multiKey = new String[]{"age", "height", "gender"}

thus, the object I would like to obtain from it would be:

TreeMap<Integer, TreeMap<Integer, TreeMap<Integer, Integer>>>

and populate it with the values from the multi key bag. So, the nested structure would contain the values from the multi key like this:

TreeMap<"age", TreeMap<"height", TreeMap<"gender", count>>> 

where "age" is replaced by the corresponding value from the bag, "height" as well and so on.. count is the number of occurrences of that particular combination (which is returned by the multi key bag itself).

Of course, the number of components of the multi key is dynamic. If the multiKey would have only two components, then the resulting object would be:

TreeMap<Integer<TreeMap<Integer, Integer>>

Retrieving the values from the bag and populating the (nested) TreeMaps does not represent an issue. Only the conversion. Any help is appreciated.

War es hilfreich?

Lösung

Instead of using a bunch of wrappers, why don't you just create your own class that groups related data together? It seems like this would very much simplify the process.

Nonetheless, If what you actually want is to be able to perform complex queries on your data (pseudo-code):

SELECT ALL (MALES >= 25 && HEIGHT < 6'1) && (FEMALES < 40 && HEIGHT > 5'0)

Then you should probably look into using a database. I'm not saying that a Tree is bad, but if your goal is to be able to easily/quickly perform complex queries, then a database is the way to go. Of course, you could write your own classes/methods to perform these calculations for you, but why reinvent the wheel if you don't have to?

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top