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

  • Home
  • SEARCH
  • 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 7824757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:42:35+00:00 2026-06-02T08:42:35+00:00

I have a need for a collection type that allows for a multiple values

  • 0

I have a need for a collection type that allows for a multiple values to be associated with multiple keys. Ideally, I would use it like Map, but map is intended for a given key to yield a single value.

Below is an example of how I would like to use this:

@Test
public void multipleItems() {
    Index<String, String> index = new Index<String, String>();
    index.add("key1", "value1");
    index.add("key2", "value2");
    index.add("key1", "value3");
    index.add("key2", "value4");
    index.add("key1", "value5");

    List<String> values = index.get("key2");

    assertEquals(2, values.size());
    assertEquals("value2", values.get(0));
    assertEquals("value4", values.get(1));
}

I am not aware of any implementation in the JVM. I could implement this myself, but I am hoping to find an existing implementation.

Recommendations?

SOLUTION

Based on the recommendations below, I ended up using the guava (and wrapping it as I didn’t want to expose this to the rest of my app in case I found a better solution.

I used these settings for including through my gradle scripts: 'com.google.guava:guava-collections:r03'

My “wrapper” class looks like this:

public class Indexes<K, V> {

    private HashMultimap<K, V> keyToValuesMap = HashMultimap.create();

    public void put(K key, V value) {
        this.keyToValuesMap.put(key, value);
    }

    public Set<V> get(K key) {
        return this.keyToValuesMap.get(key);
    }

}
  • 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-06-02T08:42:37+00:00Added an answer on June 2, 2026 at 8:42 am

    Guava, the java library from Google, has several types of Multimaps, including ListMultimap and SetMultimap, with a variety of implementations. There’s also a wiki article.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a domain collection type called SavedAnswers and would like to add that
I need a collection of keys and values (like a dictionary) but it needs
I use Java 6 Collecetions API. I need a collection that should have only
I have a collection of elements that I need to operate over, calling member
I have a collection of domain objects that I need to convert into another
I have a collection of links with thumbnails that match them. I need to
I have a Collection of type string that can contain any number of elements.
Lets say I have a custom data type that looks something like this: public
I have a collection of HTML documents for which I need to parse the
I have a collection of paths in canvas and I need to convert them

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.