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 3459100
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:03:55+00:00 2026-05-18T10:03:55+00:00

I want to store a collection of objects that are keyed based upon a

  • 0

I want to store a collection of objects that are keyed based upon a value they represent. These keys can be repeated. e.g.:

 [4] => Bob
 [5] => Mary
 [5] => Sue
 [9] => Steve
[10] => Jason
[10] => Michelle

Essentially, I want to loop through this and look at each key and say, “is there another object (person in this case) whose key is within 1 from the current key? If so, match them up and remove them from the collection.” I’m going to iterate the “1” value in the example above until the collection is empty (or has one object remaining for odd-numbered scenarios).

I’m not convinced the way I’m trying to go about this is the best way, so I’m open to feedback too.

  • 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-18T10:03:56+00:00Added an answer on May 18, 2026 at 10:03 am

    You want a Multimap. Guava provides this interface and various subinterfaces such as ListMultimap, SetMultimap and SortedSetMultimap depending on what kind of collection you want values to be stored in. It then provides various implementations such as ArrayListMultimap and HashMultimap, plus various utlities for use with them in Multimaps.

    The traditional way of doing this in Java is something like Map<K, List<V>>, Map<K, Set<V>> etc., but maintaining the values collections is tedious and various operations that should be simple (such as just putting a value for a key) are much more complicated than they need to be.

    Multimap is intended as a data structure specifically designed to model multiple values mapped to a single key (unlike Map). Given that, it makes operations as simple as you’d expect:

    ListMultimap<Integer, String> m = ArrayListMultimap.create();
    m.put(4, "Bob");
    m.put(5, "Mary");
    m.put(5, "Sue");
    ...
    
    for (String name : m.get(5)) { ... } // iterates ["Mary", "Sue"]
    

    If you wanted to ensure the same value isn’t mapped to a single key twice and don’t care about the order the values are in, you can use a SetMultimap instead of a ListMultimap, etc.

    I’m not sure what you mean as far as “is there another object whose key is within 1 from the current key? If so, match them up and remove them from the collection.” But if I’m reading it right, you could do something like this:

    for (Integer key : m.keySet()) {
      Collection<String> people = m.get(key);
      Collection<String> peopleOneLower = m.get(key - 1); // empty if there are none
      ...
    }
    

    Alternatively you could do something with a TreeMultimap<Integer, String> which will have both its key set and value sets sorted.

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

Sidebar

Related Questions

I want to implement a keyed observable collection in Silverlight, that will store unique
I want to store a a c# DateTimeOffset value in a SQL Server 2005
I need a Dictionary like object that can store multiple entries with the same
I want to store the current URL in a session variable to reference the
I want to store a large number of sound files in a database, but
I want to store the data returned by $_SERVER[REMOTE_ADDR] in PHP into a DB
I want to store values in a bunch of currencies and I'm not too
I want to store a large result set from database in memory. Every record
I want to store a very large amount of vector data on a server
I want to store a URL prefix in an Windows environment variable. The ampersands

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.