2

Hi this is a question that I found from a book. I am not exactly clear with thing happens in compareTo() method. Tree set is a sorted class. So does it always call to compareTo()?

And inside compareTo() how does sorting is done based on, return age-((Person)0).age ?

    package javaapplication1;

    import java.util.NavigableSet;
    import java.util.SortedSet;
    import java.util.TreeSet;


    public class _157 {
        public static void main(String args[]){

            NavigableSet s=new TreeSet();
        for(int i=50;i<55;++i){
        s.add(new Person(i));
        }
        SortedSet s2=s.tailSet(new Person(52));
        System.out.println(s2);
        }
    }

    class Person implements Comparable{
    private int age;
        @Override
        public int compareTo(Object t) {
           return ((Person)t).age-age;

        }

        Person(int age){

            this.age=age;

        }
        public String toString(){
        return "Person("+age+")";
        }

    }
user3789200
  • 1,166
  • 2
  • 25
  • 45
  • possible duplicate of [CompareTo Overide Sort](http://stackoverflow.com/questions/11068350/compareto-overide-sort) – Smutje Jun 30 '14 at 06:37
  • It's calculating the difference between the incoming `Person` object's age with the current instance of `Person` by subtracting them from each other – MadProgrammer Jun 30 '14 at 06:38
  • Strictly speaking, that `compareTo` method is wrong. It overflows for large negative values of `age`. Although I suppose people shouldn't have large negative ages... – Boris the Spider Jun 30 '14 at 06:38
  • At what time compareTo() is called? Is it at the moment where data is added to the TreeSet? – user3789200 Jun 30 '14 at 06:42
  • Do you understand what a `TreeSet` _is_? And how you would go about implementing one? Presumably that would answer your question. Maybe start by reading the [Wikipedia page](http://en.wikipedia.org/wiki/Red%E2%80%93black_tree). – Boris the Spider Jun 30 '14 at 06:45

1 Answers1

0

If you closely look at the TreeMap class in Java for put method, a comparison is done for new object before adding in the class.

Check this.

The compareTo method which you implemented in your Domain class is used while adding object in a Map. (FYI, TreeSet is ultimately used TreeMap to store the object in sort order).

Any modifying operation ultimately invoke the 'compareTomethod to sort the objects in Map and based on the result ofcompareTo` method, the objects are stored.

What compareTo method does in your case : It will compare the age of two person and return the difference between their age and based on this return value the TreeMap is sorted.

So if you use ((Person)t).age-age as return value, then it will store the person in descending order of their age.

Vimal Bera
  • 10,346
  • 4
  • 25
  • 47
  • 1
    This isn't right. _Any_ modifying operation on the tree _may_ result in the `compareTo` method of a particular object being called, depending on where that object is in the tree. – Boris the Spider Jun 30 '14 at 06:55
  • Thanks for replying. The output of this is [Person(52), Person(51), Person(50)]. But when I change return value into age-((Person)t).age, out put changes into [Person(52), Person(53), Person(54)]. How does this change happens. Please kindly explain. – user3789200 Jun 30 '14 at 06:58
  • @BoristheSpider yes definitely, `compareTo` method invoke on any modifying operation. I updated the answer. Thanks :) – Vimal Bera Jun 30 '14 at 07:15