Thursday, January 24, 2008

Reverse Mapping !?

I was asked to construct a macro in Confluence which actually worked as a custom RSS Feed Aggregator. The tags inside the feed had to be ordered according to the number of times the tags occurred in the xml file. The macro is written in java and I used Rome for the RSS feed reading/ traversing operations. Now the problem came when the list of tags had to be converted to some kind of a map structure which had the ordered set of tags in accordance to the number of occurrences.

Now as you know the map has a key value pairing system where the key will be unique. I managed to iterate through the list of tags converting it into a map which has the tag name as the key and its number of occurrences as the value. The problem arises when you try to reverse the map (ie the keys as the values and vice versa). Same number of occurrences overwrites the previous entered key. The solution that I thought (do not know whether it was the best on) is to translate the key with a value of a list of all the tags. This had some operations (iterations) but it did my trick. This was when I noticed that Java being a vastly used language did not have anything called a Reverse Map (at least I do not know of one ).

What I did (just an example):-
Tag list - ['work','work','fun','java','work','fun','misc']
tag to occurrences map - { 'work' -> 3 , 'fun' -> 2 , 'java' ->1, 'misc' -> 1 }
occurrences to tag map - {3 -> ['work'], 2 -> ['fun'] , 1 -> ['java','misc'] }

Sorry for the Python like analogy. The numbers are Integer Object types.

I guess the reason why Reverse Mapping is not implemented (please correct me if it is in fact implemented) is that the map takes Objects as the key / value. Then the value Object must be traversed before its made into the key. I think the check whether the Object is iterable is a solution to this. May be the rule should be if the value Object is iterable, then iterate it and translate the iterated values as the keys. Or just check if the Object is a List or a Set (simplification maybe) and if yes then iterate. Anyway I think Sun should consider implementing something like this.

Comments are welcome.

No comments: