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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:54:57+00:00 2026-05-26T08:54:57+00:00

I have a MultiMap and need the best possible way to get an EntrySet

  • 0

I have a MultiMap and need the best possible way to get an EntrySet of a MultiMap using one of the values in the list. Right now, I’m iterating through the entry set of the whole map and checking if the value of the list contains my needed values. This works for a limited number of inputs on the map, but its now turned into 800ms – 1 second work, which just wont cut it. Thanks for the help in advanced.

Example:

public static void main(String[] args) {
        MultiMap multi = new MultiHashMap();
        multi.put("Key1", new ArrayList<String>(Arrays.asList(new String[] { "Value1", "Value2", "Value3" })));
}

I want to be able to get Key1, Value1, Value2 and Value3 by only inputting Value1 and Value2 as arguments

Also, if it’s any help, this is from a cache after reading a data source

  • 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-26T08:54:57+00:00Added an answer on May 26, 2026 at 8:54 am

    [Edited to allow for group of values to be tested simultaneously]

    [Edited again now that the question has been clarified]

    This implementation avoids explicit loops, and is written in a more functional manner using Guava extensions:

    import java.util.Collection;
    import java.util.Map.Entry;
    
    import com.google.common.base.Predicate;
    import com.google.common.collect.Iterables;
    import com.google.common.collect.Multimap;
    
    public class TestIt {
    
        public static Iterable<Entry<Integer, String>> getEntrySetsForValues(
                Multimap<Integer, String> fromMap, final Collection<String> values) {
            return Iterables.filter(fromMap.entries(),
                    new Predicate<Entry<Integer, String>>() {
                        @Override
                        public boolean apply(Entry<Integer, String> arg0) {
                            return values.contains(arg0.getValue());
                        }
                    });
        }
    }
    

    A test program:

    import com.google.common.collect.HashMultimap;
    import com.google.common.collect.Multimap;
    import com.google.common.collect.Sets;
    
    public class Test {
    
        static Multimap<Integer, String> x = HashMultimap.create();
    
        public static void main(String[] args) {
            x.put(1, "a");
            x.put(1, "b");
            x.put(2, "d");
            x.put(3, "e");
            x.put(3, "f");
            x.put(4, "a");
            x.put(5, "b");
            x.put(5, "c");
    
            System.out.println(TestIt.getEntrySetsForValues(x,
                    Sets.newHashSet("a", "c")));
        }
    }
    

    The output:

    [1=a, 4=a, 5=c]

    I’d be curious to know how inefficient it is.

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

Sidebar

Related Questions

I have std::list<multimap<std::string,std::string>::iterator> > Now i have new element: multimap<std::string,std::string>::value_type aNewMmapValue(foo1,test) I want to
Suppose that I have a multimap (which maps multiple values to a single key),
I often have a need to take a list of objects and group them
Is there a simple or standard way to have a multimap iterator which iterate
I have a multimap and I would like to get a set of sets
I'm trying to have a (hash-based) Multimap with a (hash-based) Multiset of values for
I'm using a C++ std::multimap and I have to loop over two different keys.
I have to implement Priority Queue using MultiMap. I use MultiMap from Google Collections.
I have a std::multimap where key is a custom class. Something like this: Class
Have just started using Google Chrome , and noticed in parts of our site,

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.