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

The Archive Base Latest Questions

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

import java.util.*; public class Test { public static void main(String[] args) { Map<String,String> map

  • 0
import java.util.*;    
public class Test {
        public static void main(String[] args) {

            Map<String,String> map = new TreeMap<String,String>();
            map.put("10", "America");
            map.put("1", "Australia");
            map.put("2", "India");
            map.put("11", "China");

            System.out.println(map);

        }
    }

When running the above code snippet,in console I am getting the output as :

{1=Australia, 10=America, 11=China, 2=India}

But I am expecting output as

{1=Australia, 2=India, 10=America, 11=China}

But when changing the logic as mentioned below inside above main()

 Map<String,String> map = new TreeMap<String,String>();
        map.put("US", "America");
        map.put("AUS", "Australia");
        map.put("IN", "India");
        map.put("CH", "China");

    System.out.println(map);

I am getting the desired output

({AUS=Australia, CH=China, IN=India, US=America})

As per my understanding TreeMap’s entrySet() method returns a set view of the mappings contained in the map. The set’s iterator returns the mappings in ascending key order. So why this is happening in the first case?

Any suggestion is highly appreciated.

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

    Because "10" is lexicographically smaller than "2".


    Here’s a hint:

    Map<Integer,String> map = new TreeMap<Integer,String>();
    map.put(10, "America");
    map.put(1, "Australia");
    map.put(2, "India");
    map.put(11, "China");
    
    System.out.println(map);
    // {1=Australia, 2=India, 10=America, 11=China}
    

    Here’s another hint: String#compareTo(String) vs Integer#compareTo(Integer).


    Can you please explain what actually do you mean by “’10’ is lexicographically smaller than ‘2’”.

    First, read the JavaDoc I linked, especially the first link.

    Now let’s review some simple string comparisons:

    • “a” obviously comes before “b”
    • likewise “b” comes before “z”

    It shouldn’t be much of a stretch to extend this to numerical characters:

    • “0” comes before “1”
    • “1” comes before “9”

    The ordering of individual characters, such as a, b, z, 0, 1, and 9 is called their lexicographical order. In short, each character has a numerical representation which you won’t find terribly surprising.

    Now let’s look at some slightly more complicated string comparisons:

    • “aa” comes before “bb” (this shouldn’t be much of a surprise)
    • “aa” also comes before “ab”

    How did we determine the second case? Character by character.

    1. "a" is the same character as "a", so we need to keep going
    2. "a" comes before "b", so we're done.
    

    One more example: “ba” comes before “c”, since “b” comes before “c”.

    Let’s do the same thing for strings containing numerical characters:

    • Does “2” come before “10”? We compare character-by-character:

      1. Does “2” come before “1”? No, it comes after, so we’re already done.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

import java.util.Collection; public class Test { public static void main(String[] args) { Collection c
import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner sc
import java.util.Scanner; import java.lang.String; public class Test { public static void main(String[] args) {
import java.util.Arrays; public class Test { public static void main(String... args) { String[] strings
import java.util.*; public class Test { static List<Integer> list = new LinkedList<Integer>(); public static
I have a Parent class. import java.util.HashMap; import java.util.Map; public class Parent { Map<String,String>
import java.util.Scanner; public class Test { private static int decimalNum = 0; private static
I have the following Java code: import java.util.Arrays; import java.util.Collections; public class Test {
import java.lang.reflect.*; import java.util.Arrays; class Test { class Inner { public <T> Inner(T arg)
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.TimeZone; public class Test {

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.