Divyanand Kutagulla wrote:
Hi Deepak,
Thank you for your reply.
In my code I do create a comparator
I wanted to use Collections.sort method to accomplish the sorting And I wanted to be memory efficient
Yes creating a TreeMap would help was it is sorted map. but it would take additional memory, I already have a map - granted it is not sorted, but I can use the Collections.sort to sort it with the right comparator.
Divyanand
Winston Gutkowski wrote:
Really? You're creating a TreeMap, which is a binary tree structure. Since your Comparator is actually only concerned with what was in the original Map, and is going to order those keys according to their associated values, I could definitely see problems if the original Map is subsequently updated; not to mention the idea of ordering something (your String keys) by something they're not even directly associated with, AND which might be duplicated [Edit: thinking about it; that may actually be the showstopper].
It might work as you've written it (ie, with no "equal" value), and like I say, it's an interesting idea; but I'm not sure I'd want to do any predictive analysis on it.
Winston
Winston Gutkowski wrote:
1. Your compare() method doesn't check when values are equal and return 0. This might be deliberate in order to retain ALL the original key mappings, but I suspect it's flawed.
Winston Gutkowski wrote:
2. What would you expect sortedRepresentation.get("X") to return (assuming "X" is a valid key)? I suspect you'll get null more often than you think; but I have to admit, I'm not absolutely sure.