• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to use MultiKeyMap  RSS feed

 
s ravi chandran
Ranch Hand
Posts: 579
6
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a requirement where I need to lookup some data based on two different field types. Now at a time only one of the key will be present while retrieving value.

I tried MultiMap from guava library, and MultiKeyMap from apache commons, but somehow they look like solving the problem where I have multiple values.  Please let me know which data structure I can use.

I wish to have something like this:


 
Campbell Ritchie
Marshal
Posts: 56545
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does the documentation for the two kinds of Map say?
 
s ravi chandran
Ranch Hand
Posts: 579
6
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:What does the documentation for the two kinds of Map say?

Here is the example from guava docs link

produces output such as:
   Zachary: [Taylor]
   John: [Adams, Adams, Tyler, Kennedy]  // Remember, Quincy!
   George: [Washington, Bush, Bush]
   Grover: [Cleveland, Cleveland]        // Two, non-consecutive terms, rep'ing NJ!


and here is the description given there:

A collection that maps keys to values, similar to Map, but in which each key may be associated with multiple values. You can visualize the contents of a multimap either as a map from keys to nonempty collections of values:
a → 1, 2
b → 3
... or as a single "flattened" collection of key-value pairs:
a → 1
a → 2
b → 3


This is the documentation given in apache commons: link
Here is the example given:


and here is the description given:
MultiMap is a Map with slightly different semantics. Putting a value into the map will add the value to a Collection at that key. Getting a value will return a Collection, holding all the values put to that key.

This too looks like a variation of guava implementation. 

I need multiple keys returning single value object. Right now I am using two maps who have same value object. Looks crude but works.

Wish to replace it with a single data structure if I am able to find one.


 
Campbell Ritchie
Marshal
Posts: 56545
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean multiple keys or composite keys? It is very easy to use multiple keys: all you need to do is make sure each key is unique. I think you mean a composite key. Create an object which encapsulates all the keys and uses them for its hash code and equals methods.
 
s ravi chandran
Ranch Hand
Posts: 579
6
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Do you mean multiple keys or composite keys? It is very easy to use multiple keys: all you need to do is make sure each key is unique. I think you mean a composite key. Create an object which encapsulates all the keys and uses them for its hash code and equals methods.

I will be using multiple keys. Composite key will be a combination of two keys, isn't it?

What I will have is two types of string keys, both I need to map to single value object. So if I get once of the keys, i should be able to get this value object.

Currently I am doing this:

 
Campbell Ritchie
Marshal
Posts: 56545
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what is wrong with that?
 
s ravi chandran
Ranch Hand
Posts: 579
6
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:And what is wrong with that?

Just wanted to replace it with a single data structure which maybe more optimal for multi key lookups.
 
Campbell Ritchie
Marshal
Posts: 56545
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So do you want a composite key after all?
 
s ravi chandran
Ranch Hand
Posts: 579
6
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:So do you want a composite key after all?

The application constraint doesnt allow me to have a composite key, otherwise I would have added it as the first solution.

What I meant was to have some single data structure ( some variation of multimap) which abstracts out the internal handling of multiple key mapped to single value.  Maybe that might even have optimization for such a scenario.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!