1

I have very well understood the difference between Comparable and Comparator interfaces, and as such the ordering imposed by them.

Further, i am clear on why the compareTo must be consistent with equals method. From Oracle docs,

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.

For example, if one adds two keys a and b such that (!a.equals(b) && a.compareTo(b) == 0) 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.

However, I am unable to clear myself on the paragraph(below),

For the mathematically inclined, the relation that defines the natural ordering on a given class C is:

    {(x, y) such that x.compareTo(y) <= 0}.

The quotient for this total order is:

    {(x, y) such that x.compareTo(y) == 0}.

It follows immediately from the contract for compareTo that the quotient is an equivalence relation on C, and that the natural ordering is a total order on C. When we say that a class's natural ordering is consistent with equals, we mean that the quotient for the natural ordering is the equivalence relation defined by the class's equals(Object) method:

    {(x, y) such that x.equals(y)}.  

What is the author trying to suggest here?. Can someone explain with a simple example.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
Farhan stands with Palestine
  • 13,890
  • 13
  • 58
  • 105

3 Answers3

4

Basically it is saying that consistent with equals means that the set of (x, y) pairs for which x.compareTo(y) == 0 is true is identical to the set of (x, y) pairs for which x.equals(y) is true.

This isn't anything different from the common-sense understanding of consistent with equals used in the first paragraph. It is simply expressed the definition in a formal algebraic way. I'm sure that someone decided that it would be fun to add this to the docs, but I'm not sure it has any practical utility to programmers.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • What is the equivalence relation he is talking about. I couldn't clear myself through Wikipedia. – Farhan stands with Palestine Oct 22 '14 at 20:17
  • @ShirgillAnsari - There are two equivalence relations involved here. One is defined by the predicate `x.compareTo(y) == 0` (which is the quotient defined by the relation `x.compareTo(y) <= 0`) and the other is defined by the predicate `x.equals(y)`. (In the language used by Wikipedia, each predicate partitions the set of all (`x`, `y`) pairs into two cells: one for which the predicate is `true` and the other for which the predicate is `false`.) – Ted Hopp Oct 22 '14 at 20:25
  • this is exactly what i failed to understand. For ex, if i divide 8 by 4, then 2 is the quotient. So, how come x.compareTo(y) == 0 is the quotient here. – Farhan stands with Palestine Oct 22 '14 at 20:31
  • for every instance c of C you can define the equivalence set as the objects that are equal to c using the comareTo relation or equals relation. All equivalence sets of C is the set of equivalence partitions for every element of C, this set of equivalence sets is called the quotient, is has nothing to do with division. – GerritCap Oct 22 '14 at 20:39
  • Just came across this post when reading up on the contract of equals, and for what it's worth, saying that it has to define an equivalence relation on non-null objects tells me everything I need to know about it; likewise for the definition of "consistent with compareTo". – G. Bach Jul 06 '15 at 20:52
0

Let's say that you create this foolish class

public class A implements Comparable<A> {
  private int value;

  public A(int value){
    this.value = value }

  public boolean equals(Object obj){ 
     if (obj instanceof A)
        return this.value = ((A)obj).value;
     else return false;
  }

  public int compareTo(A another){
     return 0;
  }
}

Then you create two instances:

A a1 = new A(3);
A a2 = new A(4);
SortedSet<A> set = new TreeSet<A>();
set.add(a1);
set.add(a2);

The SortedSet accepts a1 but it doesn't accept a2 because a1.equals(a2) == false but a2.compareTo(a2) == 0; Remember that in a set for definition all elements are different to each other (it's like the mathemathic concept of set).

rickyalbert
  • 2,552
  • 4
  • 21
  • 31
0

It's all math ! (x,y) means " x relates to y "

An equivalence relation is a relation on a collection of elements (in this case instances of the class C) that is reflective, symmetrical and transitional.

reflective means: that every element relates to itself symmetrical means: if a relates to b then b relates to a transitional means: if a relates to b and b relates to c than a relates to c.

so the quotient relation of x.compareTo(y) == 0 is thus an equivalence relation as for each instance c of C : c.compareTo(c) is supposed to be zero, for every instance c1, c2 from C: if c1.comparTo(c2)==0 then c2.compareTo(c1) should also be zero. And for all c1, c2, c3 where c1.compareTo(c2) is zero and c2.compareTo(c3) is zero then c1.compareTo(c3) should also be zero.

A total order is a relation which is transitive (see above), anti symmetrical (again see above but now inversed as it is anti) and total hence a compareTo should reflect a total order: if a.compareTo(b) is <= 0 and b.compareTo(c) <= 0 then a.compareTo(c) must be <= 0 if a.compareTo(b) is <= 0 then b.compareTo(a) must be >= 0

total means either a.compareTo(b) <= 0 or either b.compareTo(a) <= 0, the total refers to that any element can be compared to another element hence all elements can be ordered so the ordering is total

the quotient comes from a.compareTo(b) <=0 and b.compareTo(a) <= 0 hence a.compareTo(b) == 0

I hope this is a bit more explanatory than the original...

GerritCap
  • 1,606
  • 10
  • 9
  • I just google a bit for you: total order http://en.wikipedia.org/wiki/Total_order and equivalence http://en.wikipedia.org/wiki/Equivalence_relation – GerritCap Oct 22 '14 at 20:22
  • Something else: a proper compareTo method must result in a relation that when you take the quotient (where compareTo results in 0) is a partitioning (grouping of instances of C where they are grouped in subparts of C where every couple of any 2 elements of the same partition has compareTo -> 0) only consists of couples of C that re also equals – GerritCap Oct 22 '14 at 20:26
  • Just a small quibble: usually (x,y) denotes an ordered pair, not a relation. To denote "x relates to y" one usually uses xRy (for some relation R). – Ted Hopp Oct 22 '14 at 20:32
  • Right on the spot Ted ! – GerritCap Oct 22 '14 at 20:40