0

When you have a map of string keys and sortedSet values. SortedSet values may repeat themselves between different keys. Suppose that your string keys are the dates on which you would buy groceries or something and then the sorted set would be the items that you bought on that date. Is there a way to retrieve the dates on which you bought a particular item efficiently (i.e. without looping through each key and through each item in the sortedSet)?

Naz
  • 2,012
  • 1
  • 21
  • 45
  • Well, you *will* need to iterate the keys, given the current setting, but not the values - you can invoke `contains` instead, which will end up invoking `containsKey` on the underlying `NavigableMap`. – Mena Jul 14 '16 at 10:31
  • oh yes. good idea. What would be a more optimal setting? – Naz Jul 14 '16 at 10:33
  • It really depends on your usage. One way would be to simply invert the mapping: have the items as keys and purchase dates as values. Another (more contorted) way would be to "inject" the purchase date as a property of each item in the sets, but that seems redundant. Really depends on which data correlations you ultimately need. – Mena Jul 14 '16 at 10:35

2 Answers2

0

I was just reading up in the Apache Commons Collections library. I remember that you can have a Bidirectional Map (BidiMap) in using the Collections library.

Defines a map that allows bidirectional lookup between key and values.

This extended Map represents a mapping where a key may lookup a value and a value may lookup a key with equal ease. This interface extends Map and so may be used anywhere a map is required. The interface provides an inverse map view, enabling full access to both directions of the BidiMap.

Relevant doc: https://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/BidiMap.html

The documentation also shows two known implementations provided in the Apache Commons library, OrderedBidiMap<K,V> and SortedBidiMap<K,V>.

ifly6
  • 5,003
  • 2
  • 24
  • 47
  • Read the question again: OP is looking to find a key based on a value. Using a `Map.values` invocation here is pointless on its own. – Mena Jul 14 '16 at 10:32
  • Thanks. Basically, that'd be describing an attempt to find a key based on a value: `BidiMap`. – ifly6 Jul 14 '16 at 10:36
  • makes a lot more sense now, but that sill might be overkill. If OP only wants to get a purchase date for a given item, then maybe they should just invert the mappings by using item -> purchase dates. A little to vague to know for sure as is. – Mena Jul 14 '16 at 10:38
0

With a plain Map<String,SortedSet> you can't do anything but iterate the keys and call contains on the map's value.

But you could implement your own data structure which is more optimized for your needs. Basically, with little effort, you could implement your own Map<String,SortedSet> which internally delegates to two maps - one for the basic date->item mapping and one for a reverse lookup index date->item. Then you can implement all methods needed (e.g., put which will then put the entry in both maps).

Also, have a look into this answer: Bidirectional multi-valued map in Java

Community
  • 1
  • 1
Stefan Winkler
  • 3,871
  • 1
  • 18
  • 35