-3

Taking for example these code snippets:

 public int compareTo(DSA_Student C){
    if (this.getName().compareTo(C.getName())<0) return -1;
    else
        if (this.getName().compareTo(C.getName())>0) return 1;
        else return (new Integer(this.getStudentId()).compareTo(new Integer(C.getStudentId())));
}

In the main class:

SortedSet <DSA_Student> S1=new TreeSet<DSA_Student>();
    DSA_Student a=new DSA_Student("A",10);
    DSA_Student b=new DSA_Student("F",40);
    DSA_Student c=new DSA_Student("B",49);
    DSA_Student d=new DSA_Student("E",45);
    DSA_Student e=new DSA_Student("D",30);
    DSA_Student f=new DSA_Student("C",45);
    DSA_Student g=new DSA_Student("B",45);

    S1.add(a);
    S1.add(b);
    S1.add(c);
    S1.add(d);
    S1.add(e);
    S1.add(f);
    S1.add(g);

I want to know what this.getName() and C.getName() are referring to and how does the compiler know that they are two different objects and thereby do the comparison?

Tia
  • 1,220
  • 5
  • 22
  • 47
  • The compiler **doesn't** know that they refer to two different objects, just that they refer to the `this` object and to whatever object is referred to by the C (badly named) parameter. They could in fact be one and the same object, or C may in fact be a null reference. – Hovercraft Full Of Eels Mar 05 '17 at 20:23
  • `this` is the instance on which `compareTo` is called; `C` is the parameter it was passed. For example in `a.compareTo(b)` `this` is `a`, `C` is `b`. – Andy Turner Mar 05 '17 at 20:24
  • Note: `compareTo` is not called at all, because you're passing in a `Comparator`, unless that `Comparator` happens to call `compareTo`. – Andy Turner Mar 05 '17 at 20:42
  • Maybe I didn't phrase my question well. What I intended to ask is similar to this: http://stackoverflow.com/questions/39752908/what-is-the-first-current-object-in-compareto-method-in-java-comparable-interf?rq=1 – Tia Mar 06 '17 at 13:30
  • @AndyTurner Yeah, I mistakenly used Comparator. I have edited that part in the codes – Tia Mar 07 '17 at 18:38

1 Answers1

2

this.getName() means getting name of its own object

C.getName() means getting name of object c

compareTo method used for comparing itself to other object passed

Comparable interface provides method declaration to comparing its own object to other object passed in its compareTo method.

Update :

As you have asked that working mechanism of TreeSet in using Comparable compareTo method, it's better to look into source code of TreeMap as TreeSet internally use TreeMap put method when adding object. As TreeSet keep its element in sorted order, it sorts whenever there is addition of element in TreeSet. You can have source code of TreeMap put method here. I've taken out only relevant implementation of put method which is related for using Comparable compareTo method :

            if (key == null)
                throw new NullPointerException();
            // This is where it takes your custom comparable object
            Comparable<? super K> k = (Comparable<? super K>) key; // 
            do {
                parent = t;
                // k as current object compareTo other object as 
                // t has been taken as root which changes 
                //its position based on comparsion
                cmp = k.compareTo(t.key); 
                if (cmp < 0)
                    t = t.left;
                else if (cmp > 0)
                    t = t.right;
                else
                    return t.setValue(value);
            } while (t != null);

As I am not Java expert, I would be happy to see any comment on my update to make sure that information is correct.

Akash KC
  • 16,057
  • 6
  • 39
  • 59
  • I got this, but don't `this.getName()` and `C.getName()` both point to the objects being added to TreeSet? How does the compiler know ` DSA_Student a=new DSA_Student("A",10);` or `DSA_Student b=new DSA_Student("F",40);` refers to `this.getName()` or `C.getName()`? – Tia Mar 06 '17 at 13:27
  • @Diksha : I've updated my post to answer your query in comment. – Akash KC Mar 07 '17 at 04:25