I'm looking for the most basic solution to create multiple indexes on a Java Collection.
Required functionality:
- When a Value is removed, all index entries associated with that value must be removed.
- Index lookup must be faster than linear search (at least as fast as a TreeMap).
Side conditions:
- No dependencies on large (like Lucene) libraries. No uncommon or not well tested libraries. No database.
- A library like Apache Commons Collections etc. would be ok.
- Even better, if it works with JavaSE (6.0) alone.
- Edit: No self-implemented solution (thanks for the answers suggesting this - it's good to have them here for completeness, but I already have a solution very similar to Jay's) Whenever several people find out, that they implemented the same thing, this should be part of some common library.
Of course, I could write a class that manages multiple Maps myself (that's not hard, but it feels like reinventing the wheel). So I'd like to know, if it can be done without - while still getting a simple usage similar to using a single indexed java.util.Map.
Thanks, Chris
Update
It looks very much as if we haven't found anything. I like all your answers - the self developed versions, the links to database-like libraries.
Here's what I really want: To have the functionality in (a) Apache Commons Collections or (b) in Google Collections/Guava. Or maybe a very good alternative.
Do other people miss this functionality in these libraries, too? They do provide all sorts of things like MultiMaps, MulitKeyMaps, BidiMaps, ... I feel, it would fit in those libraries nicely - it could be called MultiIndexMap
. What do you think?
If you want multiple indexes on your data, you can create and maintain multiple hash maps or use a library like Data Store:
https://github.com/jparams/data-store
Example:
With data store you can create case-insensitive indexes and all sorts of cool stuff. Worth checking out.
I'm not sure I understand the question, but I think what you're asking for is multiple ways to map from different, unique keys to values and appropriate clean-up when a value goes away.
I see that you don't want to roll your own, but there's a simple enough composition of map and multimap (I used the Guava multimap below, but the Apache one should work as well) to do what you want. I have a quick and dirty solution below (skipped the constructors, since that depends on what sort of underlying map/multimap you want to use):
removal is O(number of keys), but everything else is the same order as a typical map implementation (some extra constant scaling, since you also have to add things to the reverse).
I just used
Object
keys (should be fine with appropriate implementations ofequals()
andhashCode()
and key distinction) - but you could also have a more specific type of key.I've written a Table interface that includes methods like
We'd like to include it in a future Guava release, but I don't know when that would happen. http://code.google.com/p/guava-libraries/issues/detail?id=173
Google Collections LinkedListMultimap
About your first requirement
I think There is neither a library nor a Helper that supports it.
Here is how i have done by using LinkedListMultimap
To get your first requirement, a Helper can play a good job
...
Google collections is
lets look at project http://code.google.com/p/multiindexcontainer/wiki/MainPage This is generalized way how to use maps for JavaBean getters and perform lookups over indexed values. I think this is what you are looking for. Lets give it a try.
Basically a solution based on multiple hash maps would be possible, but in this case all of them have to be keped up-to-date manually. A very simple integrated solution can be found here: http://insidecoffe.blogspot.de/2013/04/indexable-hashmap-implementation.html