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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:51:13+00:00 2026-06-15T04:51:13+00:00

How does the TreeMap sort? say for example you have the following map: TreeMap<String,

  • 0

How does the TreeMap sort? say for example you have the following map:

TreeMap<String, Integer> treemap = new TreeMap<>();
treemap.put("lol", 1);
treemap.put("Marc", 2);
treemap.put("Jesper", 3);

Iterator ittwo = treemap.entrySet().iterator();
    while (ittwo.hasNext()) {
    Map.Entry pairs = (Map.Entry)ittwo.next();
    System.out.println(pairs.getKey() + " = " + pairs.getValue());
    ittwo.remove();
}

The output of this is:

Jesper = 3
Marc = 2
lol = 1

So if its not alphabetically what is it then?

  • 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-15T04:51:14+00:00Added an answer on June 15, 2026 at 4:51 am

    It is not only alphabetical, but it is also upper/down case sensitive.

    TreeMap<String, Integer> treemap = new TreeMap<String, Integer>();
    treemap.put("Lol", 1);
    treemap.put("Marc", 2);
    treemap.put("Jesper", 3);
    treemap.put("lol1", 1);
    treemap.put("marc1", 2);
    treemap.put("jesper1", 3);
    

    Output:

    Jesper = 3
    Lol = 1
    Marc = 2
    jesper1 = 3
    lol1 = 1
    marc1 = 2
    

    So, if you don’t need it, you can use your custom comparator, and compare string in lower case:

    TreeMap<String, Integer> treemap = new TreeMap<String, Integer>(new Comparator<String>() {
        public int compare(String o1, String o2) {
            return o1.toLowerCase().compareTo(o2.toLowerCase());
        }
    });
    treemap.put("Lol", 1);
    treemap.put("Marc", 2);
    treemap.put("Jesper", 3);
    treemap.put("lol1", 1);
    treemap.put("marc1", 2);
    treemap.put("jesper1", 3);
    

    Output:

    Jesper = 3
    jesper1 = 3
    Lol = 1
    lol1 = 1
    Marc = 2
    marc1 = 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Java I can do something like the following: TreeMap<Double, String> myMap = new
why does this run: static TreeMap<String, int[]> configs = new TreeMap<String, int[]>(); int[] upperarms_body
I just have a map implementation below to sort the values in descending order
Does the Swing API have classes which combine InputVerifiers? For example, I have three
I want to create a map Integer to String , and this map order
I have a Map keyed by Integer. Using EL, how can I access a
why does Guava doesn't have the following factory call to create a MultiMap from
I have the following function which should return a map of dates to longs
I have written a TrieMap<V> implements Map<String,V> class which is obviously keyed from Strings.
I'm trying to get familiar with Collections. I have a String which is my

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.