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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:29:39+00:00 2026-05-23T01:29:39+00:00

I often get into situation when I need to sort a Map on values.

  • 0

I often get into situation when I need to sort a Map on values. Maps are not meant for that in JDK and I decided not to use Guava (seems like this stuff is one liner but I didn’t quite get it) nor Apache Commons, so I do it this way. Btw this is a very popular question, but most of the answers are wrong in one way or another.

    Map<String, Long> map = new HashMap<String, Long>();
    // populate
    List<Map.Entry<String, Long>> list = new LinkedList<Map.Entry<String,Long>>();
    for (Map.Entry<String, Long> entry : map.entrySet()) {
        list.add(entry);
    }
    Collections.sort(list, new MapComparable());
    LinkedHashMap<String, Long> linkedMap = new LinkedHashMap<String, Long>();

    for (Map.Entry<String, Long> entry : list) {
        linkedMap.put(entry.getKey(), entry.getValue());
    }
}

    public static class MapComparable implements Comparator<Map.Entry<String, Long>>{

        public int compare(Entry<String, Long> e1, Entry<String, Long> e2) {
            return (e1.getValue()<e2.getValue() ? -1 : (e1.getValue()==e2.getValue() ? 0 : 1));
        }
    }

My question is, is there a better way of getting the EntrySet to / from Collection ? It doesn’t look good.

And is this reliable ?

  • 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-23T01:29:39+00:00Added an answer on May 23, 2026 at 1:29 am

    What I think is a very slight improvement to your method is:

    Queue queue = new PriorityQueue( map.size(), new MapComparable() );
    
    queue.addAll( map.entrySet() );
    
    LinkedHashMap<String, Long> linkedMap = new LinkedHashMap<String, Long>();
    
    for (Map.Entry<String, Long> entry; (entry = queue.poll())!=null;) {
        linkedMap.put(entry.getKey(), entry.getValue());
    }
    

    In other words, do the sorting with a datastructure designed for sorting.

    As a general note, code like

    for (Map.Entry<String, Long> entry : map.entrySet()) {
        list.add(entry);
    }
    

    Can be shortened to:

    list.addAll( map.entrySet() );
    

    whenever you’re dealing with Collections.

    Also I think this:

    public int compare(Entry<String, Long> e1, Entry<String, Long> e2) {
        return e1.getValue().compareTo(e2.getValue());
    }
    

    is cleaner.

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

Sidebar

Related Questions

I often have data in Excel or text that I need to get into
I often get into situation when I'd like to use template method pattern, but
I'm using .NET typed datasets on a project, and I often get into situations
Personally, I've found that when good developers deal with clients, they often get sucked
When databinding my xaml to some data I often use the get part of
In my projects i often use get params as a temporary way to test
I often get a problem with Windows Installer trying to uninstall a package, but
We get this error in Visual Studio 2005 and TFS very often. Can anyone
Often when making changes to a VS2008 ASP.net project we get a message like:
Quite often, in programming we get situations where null checks show up in particularly

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.