maintaining multiple sort orders on a collection
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Sharon_Stern
Posted On:   Monday, February 18, 2002 03:21 PM

I have a hashmap of objects, stored with a String as the key. Now I've discovered that I need to be able to do a lookup on these objects in two ways (by category and by first letter of the object_id).My lookup operation should return a sorted set of all values matching the lookup parameter, so all objects starting with the letter "n" or of category "z". At first glance, it seemed that it would be more efficient to create some kind of structure to index the dataset for each lookup, as opposed to iterating through my entire hashmap looking for matches. But now that I've browsed the API, I'm not so sure that's true. Is there a nic   More>>

I have a hashmap of objects, stored with
a String as the key. Now
I've discovered that I need to be able
to do a lookup on these objects in two
ways (by category and by first letter of
the object_id).My lookup operation
should return a sorted set of all values matching the lookup parameter, so all objects starting with the letter "n" or of category "z".


At first glance, it seemed that it would
be more efficient to create some kind of
structure to index the dataset for each
lookup, as opposed to iterating through
my entire hashmap looking for matches.
But now that I've browsed the API, I'm
not so sure that's true. Is there a nice
way to create indexes of my collection
data? If so what's the right data
structure to use? This collection of
objects doesn't get changed once it's
initialized, so adding and deleting
aren't concerns.


Thanks for any help,


Sharon

   <<Less

Re: maintaining multiple sort orders on a collection

Posted By:   Christopher_Schultz  
Posted On:   Monday, February 18, 2002 05:22 PM

If the data are fairly static, there's no reason not to keep each "index" in a separate data structure.



Don't forget that the data will be shared, so the overhead of, say, two HashMaps will be justified given the performance increase for lookups.



Iterating through a data set can be time consuming. I suggest using a sorted Collection that uses a custom Comparator (for each index), and you'll probably get log2 complexity for lookups in, say, a tree.



-chris
About | Sitemap | Contact