13

I stumbled upon multi_index on a lark last night while pounding my head against a collection that I need to access by 3 different key values, and also to have rebalancing array semantics. Well, I got one of my two wishes (3 different key values) in boost::multi_index.

Does anything similar exist in the Java world?

bruno
  • 2,213
  • 1
  • 19
  • 31
Chris K
  • 11,996
  • 7
  • 37
  • 65
  • may be overkill (hence comment), have you considered an embedded DB for this? There are lots of light weight DBs that you can embed and run in-memory, I guess it depends on your performance requirements.... – Nim May 20 '13 at 09:51

5 Answers5

2

Resurrecting an old question, but take a look at CQEngine as a solution.

For background also see related question How do you query object collections in Java (Criteria/SQL-like)?

Community
  • 1
  • 1
npgall
  • 2,979
  • 1
  • 24
  • 24
2

I have just finished MultiIndexContainer in Java: http://code.google.com/p/multiindexcontainer/wiki/MainPage. I know that it is not complete equivalent of boost multi_index_container but maybe it could be sufficient for your requirement.

Fekete Kamosh
  • 361
  • 1
  • 5
  • 18
  • interface looks nice and actually solves the multi index requirement. – Alexander Oh May 25 '17 at 08:19
  • Wow, maybe time to resurrect my old project after so long time:-) BTW I have exported it to github https://github.com/Kamosh/multiindexcontainer but hard to say in what shape it is. – Fekete Kamosh May 26 '17 at 09:44
1

I think the short answer is no, there's no obvious equivalent.

The boost multi-index class is very heavily templated, which isn't easily translatable in Java. There are generics, but they're not at all the same. ( How are Java generics different from C++ templates? Why can't I use int as a parameter? ).

So without templating, what would the multi-index class look like?

I imagine you would have your data class, e.g. Person, containing index members like a Map implementation. At this point, you have a choices:

  1. Add some "indexes" directly to the Person class (like some Hashtables) and write lookup functions. Manage index synchronization within the Person class.
  2. Write an "IndexProvider" class that decouples the index functionality entirely from Person - it would have to be able to dynamically create different index types and I would imagine you would handle synchronization via callbacks.
  3. Some mix of 1) and 2) - like an abstract base class for index functionality, which doesn't properly decouple the behaviour but does provide some code reuse.

I think, in the majority of cases 1) is the easiest to write, easiest to maintain and is probably the most performant. 2) seems like over-engineering.

The other option, if you have a lot of data structures that need indexing, is to store them in a database.

Community
  • 1
  • 1
Tim Gee
  • 11
  • 1
-1

I think you can find for the answer in google guava library. Probably multimaps solve your needs.

https://code.google.com/p/guava-libraries/wiki/CollectionUtilitiesExplained

As commented in these threads:

Community
  • 1
  • 1
gavioto
  • 1,695
  • 2
  • 17
  • 38
  • `multi_index` means that you can put a value with multiple keys inside and have a grouping by each key. imagine something like a map of files, once keyed by name and once keyed by extension and so on. – Alexander Oh May 24 '17 at 15:24
-2

I have no idea what boost::multi_index means, but based on the rest of your question, I think you might be talking about a multi key map

Fortega
  • 19,463
  • 14
  • 75
  • 113
  • Nope, sorry. That's an example of a weakly-typed, composite key container. boost::multi_index is strongly typed, but more importantly it has multiple, independent keys. I.e. in a multi_index_container of persons, you could look up a person by name, SSN, or date of birth. (obviously the name and DOB won't be unique keys) – MSalters Sep 02 '10 at 11:42