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

  • Home
  • SEARCH
  • 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 8431247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:48:12+00:00 2026-06-10T05:48:12+00:00

From the JavaDoc of TreeMap : Note that the ordering maintained by a sorted

  • 0

From the JavaDoc of TreeMap :

Note that the ordering maintained by a sorted map (whether or not an
explicit comparator is provided) must be consistent with equals if
this sorted map is to correctly implement the Map interface. (See
Comparable or Comparator for a precise definition of consistent with
equals.) This is so because the Map interface is defined in terms of
the equals operation, but a map performs all key comparisons using its
compareTo (or compare) method, so two keys that are deemed equal by
this method are, from the standpoint of the sorted map, equal. The
behavior of a sorted map is well-defined even if its ordering is
inconsistent with equals; it just fails to obey the general contract
of the Map interface.

Can some one give an concrete example to demonsrate the problem that might occur if ordering is not consistent with equals ? Take for example User defined class that has a natural ordering i.e it implements Comparable . Also do all internal classes in JDK maintain this invariant?

  • 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-10T05:48:13+00:00Added an answer on June 10, 2026 at 5:48 am

    The contract of the Comparable interface allows for non-consistent behaviour:

    It is strongly recommended (though not required) that natural orderings be consistent with equals.

    So in theory, it is possible that a class in the JDK had a compareTo not consistent with equals. One good example is BigDecimal.

    Below is a contrived example of a comparator that is not consistent with equals (it basically says that all strings are equal).

    Output:

    size: 1
    content: {a=b}

    public static void main(String[] args) {
        Map<String, String> brokenMap = new TreeMap<String, String> (new Comparator<String>() {
    
            @Override
            public int compare(String o1, String o2) {
                return 0;
            }
        });
    
        brokenMap.put("a", "a");
        brokenMap.put("b", "b");
        System.out.println("size: " + brokenMap.size());
        System.out.println("content: " + brokenMap);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

from the javadoc for java.lang.Process : The methods that create processes may not work
I see from the javadoc that the @SuppressWarnings annotation applies to TYPE,FIELD,METHOD,PARAMETER,CONSTRUCTOR,LOCAL_VARIABLE targets. Why
From the JavaDoc : returns: The String that is the result of evaluating the
I would like class B to inherit the Javadoc from an interface that it
In the JavaDoc of ActivityUnitTestCase it says: Do not call from your setUp() method.
From Javadoc: Hash table and linked list implementation of the Map interface, with predictable
I am wondering whether or not to commit Javadoc files to my project's SVN
From the javadoc of Calendar.before(Object when) : Returns whether this Calendar represents a time
From the JavaDoc, the EXTRA_STREAM parameter when launching an intent needs to be an
I am generating the Javadoc from my project with gradle, and I want to

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.