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?
Use Prefuse Tables. They support as many indices as you want, are fast (indices are TreeMaps), and have nice filtering options (boolean filters? no problem!). No database required, tested with large data-sets in many information visualization applications.
In their raw form, they are not as convenient as standard containers (you need to deal with rows and columns), but you can surely write a small wrapper around that. Plus, they plug nicely into UI components such as Swing's JTables.
Take a look at CQEngine (Collection Query Engine), it's an exact fit for this kind of requirement, being based around an
IndexedCollection
.Also see related question How do you query object collections in Java (Criteria/SQL-like)? for more background.