1

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.

Marius
  • 65
  • 2
  • 9
  • It is not clear how you get the nested tree structure from `multiKey` and how they relate. Can you elaborate your question ? May be there is a composite data structure that can better suit your needs. – Deepak Bala Jul 09 '13 at 09:58
  • @DeepakBala that is what I am trying to obtain. A nested tree structure from the multiKey. The number of nested TreeMaps being dependent on the number of components of the multiKey. – Marius Jul 09 '13 at 10:05
  • @Marius That didn't answer the question. How is it nested? – Steve P. Jul 09 '13 at 10:06
  • @SteveP. OK. I've edited the question. i hope is more clear now. The population of the nested TreeMaps it is not an issue. Only how to initialize/create a nested TreeMap structure dependent on the number of components in the multi key. – Marius Jul 09 '13 at 10:13

1 Answers1

0

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?

Steve P.
  • 14,489
  • 8
  • 42
  • 72
  • Yes, that is what I was realizing, that an embedded database would solve the problem and create a lot more flexibility. Creating my own class for grouping is not the choice for it, as I do not know all the possibilities/functionality it should provide and it would imply modifying/adding new methods every time a user comes up with a new question. Thanks for your effort. – Marius Jul 09 '13 at 12:08
  • No problem. If you want a purely java database, check out [H2](http://www.h2database.com/html/main.html). – Steve P. Jul 09 '13 at 12:12
  • Will do, thanks again. Sorry can't give you points.. not enough reputation :/ – Marius Jul 09 '13 at 12:19