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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:35:01+00:00 2026-06-18T02:35:01+00:00

This is not basically how to sort the HashMap based on keys. For that

  • 0

This is not basically how to sort the HashMap based on keys. For that I could directly use TreeMap without a wink 🙂

What I have at the moment is

Map<String, Object> favoritesMap = new HashMap<String, Object>();
and its contents can be
["Wednesdays" : "abcd"]
["Mondays" : "1234"]
["Not Categorized" : "pqrs"]
["Tuesdays" : "5678"]

I want to sort the HashMap based on keys and additional to this I need “Not Categorized” to be the last one to retrieve.

So expected while iterating over keySet is

["Mondays", "Tuesdays", "Wednesdays", "Not Categorized"] i.e. sorted on keys and "Not Categorized" is the last one

Thought of going for HashMap while creating and at the end add ["Not Categorized" : "pqrs"] but HashMap does not guarantee the order 🙂

Any other pointers for the solution?

  • 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-18T02:35:02+00:00Added an answer on June 18, 2026 at 2:35 am

    Are you specifically excluding TreeMap for some external reason? If not you could obviously use TreeMap with a specially made Comparator.

    Have you considered any of the other SortedMaps?

    If TreeMap is definitely out I would extend HashMap and make it look like there is always one more entry but that is certainly not a trivial piece of work. You should have a very good reason not to use a SortedMap before going down this road.

    Added

    Here is an example of how you can make a particular entry always sort to the end using a TreeMap:

    // This key should always appear at the end of the list.
    public static final String AtEnd = "Always at the end";
    
    // A sample map.
    SortedMap<String, String> myMap =
            new TreeMap<>(
            new Comparator<String>() {
              @Override
              public int compare(String o1, String o2) {
                return o1.equals(AtEnd) ? 1 : o2.equals(AtEnd) ? -1 : o1.compareTo(o2);
              }
            });
    
    private void test() {
      myMap.put("Monday", "abc");
      myMap.put("Tuesday", "def");
      myMap.put("Wednesday", "ghi");
      myMap.put(AtEnd, "XYZ");
    
      System.out.println("myMap: "+myMap);
      // {Monday=abc, Tuesday=def, Wednesday=ghi, Always at the end=XYZ}
    }
    

    I wonder if you are looking for some variant of that?

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

Sidebar

Related Questions

I am not sure whether this only happens to me. Basically if I have
I'm just not too sure the proper way of doing this. I basically have
Basically, I want this sort of functionality but less complicated: http://www.blueacorn.com/team/ I have a
Afternoon, How would i set this as not equal to? I am basically trying
I hope this question is not 'controversial' - I'm just basically asking - has
Hello not sure why Im getting this error. Basically I get it in these
I'm not sure if this is possible or not. Basically, I'm writing a script
This is a very odd one and I'm not sure how to proceed. Basically,
I'm not sure if this is even what you call it, but basically, I
I basically want to do this in code: PersonList myPersonList; //populate myPersonList here, not

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.