Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6922331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:23:33+00:00 2026-05-27T10:23:33+00:00

I am looking for a way to store key-value pairs. I need the lookup

  • 0

I am looking for a way to store key-value pairs. I need the lookup to be bidirectional, but at the same time I need to store multiple values for the same key. In other words, something like a BidiMap, but for every key there can be multiple values. For example, it needs to be able to hold pairs like: “s1”->1, “s2”->1, “s3”->2, and I need to be able to get the value mapped to each key, and for each value, get all the keys associated with it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:23:33+00:00Added an answer on May 27, 2026 at 10:23 am

    So you need support for many-to-many relationships? Closest you can get is Guava‘s Multimap like @Mechkov wrote – but more specifically Multimap combination with Multimaps.invertFrom. “BiMultimap” isn’t implemented yet, but there is an issue requesting this feature in Google Guava library.

    At this point you have few options:

    1. If your “BiMultimap” is going to immutable constant – use Multimaps.invertFrom and ImmutableMultimap / ImmutableListMultimap / ImmutableSetMultimap (each of theese three has different collection storing values). Some code (example taken from app I develop, uses Enums and Sets.immutableEnumSet):

      public class RolesAndServicesMapping {
          private static final ImmutableMultimap<Service, Authority> SERVICES_TO_ROLES_MAPPING = 
               ImmutableMultimap.<Service, Authority>builder()
                  .put(Service.SFP1, Authority.ROLE_PREMIUM)
                  .put(Service.SFP, Authority.ROLE_PREMIUM)
                  .put(Service.SFE, Authority.ROLE_EXTRA)
                  .put(Service.SF, Authority.ROLE_STANDARD)
                  .put(Service.SK, Authority.ROLE_STANDARD)
                  .put(Service.SFP1, Authority.ROLE_ADMIN)
                  .put(Service.ADMIN, Authority.ROLE_ADMIN)
                  .put(Service.NONE, Authority.ROLE_DENY)
                  .build();
      
          // Whole magic is here:
          private static final ImmutableMultimap<Authority, Service> ROLES_TO_SERVICES_MAPPING =
                  SERVICES_TO_ROLES_MAPPING.inverse();
          // before guava-11.0 it was: ImmutableMultimap.copyOf(Multimaps.invertFrom(SERVICES_TO_ROLES_MAPPING, HashMultimap.<Authority, Service>create()));
      
          public static ImmutableSet<Authority> getRoles(final Service service) {
              return Sets.immutableEnumSet(SERVICES_TO_ROLES_MAPPING.get(service));
          }
      
          public static ImmutableSet<Service> getServices(final Authority role) {
              return Sets.immutableEnumSet(ROLES_TO_SERVICES_MAPPING.get(role));
          }
      }
      
    2. If you really want your Multimap to be modifiable, it will be hard to maintain both K->V and V->K variants unless you will be modifying only kToVMultimap and call invertFrom each time you want to have its inverted copy (and making that copy unmodifiable to make sure that you accidentally don’t modify vToKMultimap what wouldn’t update kToVMultimap). This is not optimal but should do in this case.

    3. (Not your case probably, mentioned as bonus): BiMap interface and implementing classes has .inverse() method which gives BiMap<V, K> view from BiMap<K, V> and itself after biMap.inverse().inverse(). If this issue I mentioned before is done, it will probably have something similar.

    4. (EDIT October 2016) You can also use new graph API which will be present in Guava 20:

      As a whole, common.graph supports graphs of the following varieties:

      • directed graphs
      • undirected graphs
      • nodes and/or edges with associated values (weights, labels, etc.)
      • graphs that do/don’t allow self-loops
      • graphs that do/don’t allow parallel edges (graphs with parallel edges are sometimes called multigraphs)
      • graphs whose nodes/edges are insertion-ordered, sorted, or unordered
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for the most efficient way to store key value pairs in a
I am looking for a way to store a key-value pair in the application
I'm looking for a way to maintain the sorting on my key-value pairs. They
In Java I'm looking for a way to map multiple keys to the same
Which is a more efficient way of accessing (key,value) pairs in terms of both
I want to store a bunch of key value pairs, with the key being
For a few projects I'm working on I need a persistent key value store
I am looking for a solution that lets me store and lookup multiple tuples.
I am looking for a way to store references to variables inside a NSMutableArray.
I am looking for a neat/clean way to store a list of strings into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.