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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:35:41+00:00 2026-05-29T07:35:41+00:00

When I need to sort a HashMap by value, the advice seems to be

  • 0

When I need to sort a HashMap by value, the advice seems to be to create the HashMap and then put the data into a TreeMap which is sorted by value.

For example: Sort a Map<Key, Value> by values (Java)

My question: why is it necessary to do this? Why not create a TreeMap(which is sorted by keys) and then sort it in place by value?

  • 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-29T07:35:42+00:00Added an answer on May 29, 2026 at 7:35 am

    If you know your values to be unique, you can use Guava’s BiMap (bidirectional map) to store the data. Create a HashBiMap as you would your HashMap, then create a new TreeMap from its inverse:

    new TreeMap<>(biMap.inverse());
    

    That map will then be sorted by the values. Remember that what you’re thinking of as “keys” and “values” will be swapped.

    If your values are not unique, you can create a multimap of the inverse. A multimap is essentially a mapping from each key to one or more values. It’s usually implemented by making a map from a key to a list. You don’t have to do that though, because Google did it for you. Just create a multimap from your existing map, and ask Guava to invert it for you into a TreeMultimap, which, as you can guess, is a TreeMap that can hold multiple values per key.

    Multimaps.invertFrom(Multimaps.forMap(myMap), new TreeMultimap<V, K>());
    

    Multimap documentation is provided.

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

Sidebar

Related Questions

I have an array which i need to sort its elements by occurrence then
I need to sort many files and then dump into many files in order
I need to sort on a date-field type, which name is mod_date. It works
Sorry for my English. I need sort nodes of this XML data <root> <section
I need to sort an array containing arrays in ascending numerical order. The data
I need to sort my HashMap according to the values stored in it. The
I often get into situation when I need to sort a Map on values.
I need to sort an array of ActiveRecord objects by the value in one
I need to sort a data frame by date in R. The dates are
I need to sort by value instead of Key, I think.... Heres where I

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.