0

I have the following BiMap collections:

BiMap<String,String> accessIds = HashBiMap.create();
accessIds.put("FOO","accessId 1"); //This access Id is common to both FOO and BAR

BiMap<String,String> merchants = HashBiMap.create();
merchants.put("FOO", "merchant 1"); //Both FOO and BAR each have unique merchants
merchants.put("BAR", "merchant 2");

These are 2 of the 4 total collections I currently have. All 4 collections share the same keys, but different values.

The question I have is: How can I ensure that I can get merchant 2 when I have an accessIds key of FOO?

Before someone points out that these two collections do not, in fact, share the same keys, please remember that a BiMap enforces unique values so I am unable to list "BAR","accessId 1" in the collection.

I'm not convinced that BiMap is the right collection, but I do make use of its inverse() method. If there is a collection better suited ( or some other method that I am overlooking ) please let me know.

FYI: I use Guava-14.0-rc1 for the BiMap collection.

Robert H
  • 11,520
  • 18
  • 68
  • 110
  • Tell us your use case, instead of a non-working solution. If several keys can have the same value in accessIds, obviously it can't be a BiMap. And why would FOO or BAR have precedence on the other if they share the same accessId? Why not use real objects? – Frank Pavageau Feb 11 '13 at 23:11
  • @FrankPavageau In my other cases accessId's can be directly mapped to a specific value. My use case takes an Access ID, and then grabs several pieces of data from various collections to build the objects required to authenticate to various webservices. I didn't put any other specific information in as this snippet, because even though it doesn't represent real information,it serves to illustrate what I need to accomplish. As I stated in the question, I'm not convinced that BiMap is correct, so if you have another suggestion, please let me know. – Robert H Feb 12 '13 at 02:05

1 Answers1

1

Based on your comment, in your workflow, the Access ID is a key, not a value, that in at least one case has several associated values instead of one.

You could use a Multimap for your Access IDS, assuming you can then select which value to retain as the key for accessing the other Maps (or BiMaps, though it's unclear through your example why they are BiMaps, but I guess that's unrelated).

ImmutableMultimap.Builder<String, String> builder = ImmutableMultimap.builder();
builder.put("FOO", "accessId 1");
builder.put("BAR", "accessId 1");
ImmutableMultimap<String, String> accessIds = builder.build();
ImmutableMultimap<String, String> byAccessIds = accessIds.inverse();

Collection<String> keys = byAccessIds.get("accessId 1"); // ["FOO", "BAR"]
String key = doSomething(keys); // "BAR" is chosen
String merchant = merchants.get(key); // "merchant 2"

If you cannot use immutable structures, you can also build a regular Multimap for accessIds (for example using a HashMultimap) and inverse it using Multimaps.invertFrom().

Frank Pavageau
  • 11,477
  • 1
  • 43
  • 53