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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:05:45+00:00 2026-06-15T09:05:45+00:00

private Map<Character, Integer> frequencies; I have a Map with Character being Key and its

  • 0
private Map<Character, Integer> frequencies;

I have a Map with Character being Key and its associated Integer being Value.

Whats the best/fastest/efficeint way to sort by Value?

i.e Map may have
a,1
c,10
p,5
s,7
and after sorted,
it would be
a,1
p,5
s,7
c,10

I was thinking about doing it with Priority Queue and with integer but i would lose the Character value if the integer vals are duplicates

  • 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-06-15T09:05:46+00:00Added an answer on June 15, 2026 at 9:05 am

    A priority queue is a decent approach – all you need to do is get the Entry set from the map, and override a Comparator as the input to the queue.

    Map<Character,Integer> map = new HashMap<Character, Integer>();
    map.put('a',1);
    map.put('c',10);
    map.put('p',5);
    map.put('2',7);
    PriorityQueue<Entry<Character, Integer>> pq = new PriorityQueue<Map.Entry<Character,Integer>>(map.size(), new Comparator<Entry<Character, Integer>>() {
    
        @Override
        public int compare(Entry<Character, Integer> arg0,
                Entry<Character, Integer> arg1) {
            return arg0.getValue().compareTo(arg1.getValue());
        }
    });
    pq.addAll(map.entrySet());
    while (!pq.isEmpty()) {
        System.out.println(pq.poll());
    }
    

    Will yield (as expected):

    a=1
    p=5
    2=7
    c=10
    

    Note: Avoid using a Set or a Map with keys as the values of the map – because it will NOT handle duplicate values well.

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

Sidebar

Related Questions

I have the following code private Map<KEY, Object> values = new HashMap<KEY, Object>(); public
I have a map of constants, like this: private static Map<String, Character> _typesMap =
Supposing I have the following public class Foo { private Map<Integer,SomeObject> myMap; public Foo()
If map is defined as private Map<Integer, Integer> map = new HashMap<Integer, Integer>(); Updating
I have a class like the one below: class Foo { private: std::map<std::string, Bar*>
I have a class like this: class MyClass{ public: MyClass(int Mode); private: std::map <
I have a static map that is a private data member. How do I
let's say I have std::map< std::string, std::string > m_someMap as a private member variable
I have an object that contains public class PositionsChannelApplicationGroups { public PositionsChannelApplicationGroups(){} private Map<MyObj1,
I have a template class with a private map member template <typename T> class

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.