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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:51:50+00:00 2026-05-26T11:51:50+00:00

If I have a Guava Multimap, how would I sort the entries based on

  • 0

If I have a Guava Multimap, how would I sort the entries based on the number of values for the given key?

For instance:

Multimap<String, String> multiMap = ArrayListMultimap.create();
multiMap.put("foo", "1");
multiMap.put("bar", "2");
multiMap.put("bar", "3");
multiMap.put("bar", "99");

Given this, when iterating over multiMap, how would I get the “bar” entries to come first (since “bar” has 3 values vs. only 1 for “foo”)?

  • 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-26T11:51:51+00:00Added an answer on May 26, 2026 at 11:51 am

    Extract the entries in a list, then sort the list :

    List<Map.Entry<String, String>> entries = new ArrayList<Map.Entry<String, String>>(map.entries());
    Collections.sort(entries, new Comparator<Map.Entry<String, String>>() {
        @Override
        public int compare(Map.Entry<String, String> e1, Map.Entry<String, String> e2) {
            return Ints.compare(map.get(e2.getKey()).size(), map.get(e1.getKey()).size());
        }
    });
    

    Then iterate over the entries.

    Edit :

    If what you want is in fact iterate over the entries of the inner map (Entry<String, Collection<String>>), then do the following :

    List<Map.Entry<String, Collection<String>>> entries = 
        new ArrayList<Map.Entry<String, Collection<String>>>(map.asMap().entrySet());
    Collections.sort(entries, new Comparator<Map.Entry<String, Collection<String>>>() {
        @Override
        public int compare(Map.Entry<String, Collection<String>> e1, 
                           Map.Entry<String, Collection<String>> e2) {
            return Ints.compare(e2.getValue().size(), e1.getValue().size());
        }
    });
    
    // and now iterate
    for (Map.Entry<String, Collection<String>> entry : entries) {
        System.out.println("Key = " + entry.getKey());
        for (String value : entry.getValue()) {
            System.out.println("    Value = " + value);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a guava HashMultimap like this Multimap<String, String> multiMap = HashMultimap.create(); multiMap.put(a, x);
I would like to have a c.g.c.c.Multimap that is sorted based on keys only.
why does Guava doesn't have the following factory call to create a MultiMap from
I have a multiset in guava and I would like to retrieve the number
I have a maven-based GWT project that includes Guava. I am running into trouble
Using Guava, I would like to have a map that has the following properties:
I have a project which would like to utilize the Google Guava libraries (on
I have class implementing Comparator<String> which should sort strings in such manner that strings
Have you determined a maximum number of characters allowed in FCKEditor ? I seem
I have the java guava library, and was wondering if they have a tryparse

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.