title: Equivalence relation and natural ordering on a class in Java
body: <p>I have very well understood the difference between Comparable and Comparator interfaces, and
  as such the ordering imposed by them.</p>
  
  <p>Further, i am clear on why the compareTo must be consistent with equals method.
  From <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html" rel="nofollow">Oracle docs</a>, </p>
  
  <blockquote>
    <p>It is strongly recommended (though not required) that natural orderings be consistent
        with equals. This is so because sorted sets (and sorted maps) without explicit 
        comparators behave "strangely" when they are used with elements (or keys) 
        whose natural ordering is inconsistent with equals. In particular, such a 
        sorted set (or sorted map) violates the general contract for set (or map), 
        which is defined in terms of the equals method.</p>
    
    <p>For example, if one adds two keys <code>a</code> and <code>b</code> such that 
        <code>(!a.equals(b) &amp;&amp; a.compareTo(b) == 0)</code> to a sorted set that does not use an 
        explicit comparator, the second add operation returns false (and the size of 
        the sorted set does not increase) because a and b are equivalent from the 
        sorted set's perspective.</p>
  </blockquote>
  
  <p>However, I am unable to clear myself on the paragraph(below),</p>
  
  <blockquote>
    <p>For the mathematically inclined, the <em>relation</em> that defines the natural ordering on 
        a given class C is:</p>
  
  <pre><code>    {(x, y) such that x.compareTo(y) &lt;= 0}.
  </code></pre>
    
    <p>The <em>quotient</em> for this total order is:</p>
  
  <pre><code>    {(x, y) such that x.compareTo(y) == 0}.
  </code></pre>
    
    <p>It follows immediately from the contract for <code>compareTo</code> that the quotient is an
        <em>equivalence relation</em> on C, and that the natural ordering is a total order on C. 
        When we say that a class's natural ordering is <em>consistent with equals</em>, we mean 
        that the quotient for the natural ordering is the equivalence relation defined 
        by the class's <code>equals(Object)</code> method:</p>
  
  <pre><code>    {(x, y) such that x.equals(y)}.  
  </code></pre>
  </blockquote>
  
  <p>What is the author trying to suggest here?. Can someone explain with a simple example.</p>