简体   繁体   中英

Comparable and Comparator Interface

As I was going through the above interfaces I am not much clear about the syntax of these Interfaces after reading many sites on same topic.

Consider the following code snippet :

public class ComparableTest implements Comparable,Comparator {

    String name;
    int age;

    public ComparableTest(String name,int age){
        this.name=name;
        this.age=age;
    }
    @Override
    public int compareTo(Object o){               // line 1
        ComparableTest c=(ComparableTest)o;
        return name.compareTo(c.name);            // line 2
    }
@Override
public int compare(Object o1, Object o2){        // line 4
    ComparableTest c1=(ComparableTest)o1;
    ComparableTest c2=(ComparableTest)o2;
    return return c1.name.compareTo(c2.name);
}

    public static void main(String[] args) {
        ComparableTest ct1=new ComparableTest("Max",23);
        ComparableTest ct2=new ComparableTest("Alex",22);
        ComparableTest ct3=new ComparableTest("Zen",25);
        List lt=new ArrayList();
        lt.add(ct1);
        lt.add(ct2);
        lt.add(ct3);
        Collections.sort(lt);                                 // line 3
        Collections.sort(lt,new ComparableTest("jack",98));   // line 5
    }
}

1) In line 3, Collections.sort(lt) with list as parameter calls compareTo which is having Object as parameter accepts the list lt. How ? Isn't the compareTo must have a List as Parameter. I know List is also type of Object but how Object o will accepts that List which contains an instances of some class.(Please exclude generics as i don't know now)

2) Suppose I call c1.comapreTo(c2) then it is clear c1 is this object and c2 is another object for comparison. So then in comapreTo(Object o) the following line is crystal clear

public int compareTo(Object o) {
     //cast the Object o
     return c1.name.compareTo(c2.name);
}

But In line 2, I wrote only name.compareTo(c.name) and compared. What does name refer here. How Sorting is taking place? I read that it is current object but we call compareTo with Collections.sort not with any object.

3) As line 5 calls line 4, line 4 takes list lt as o1 and newly Object created as o2. I don't understand the object to be sorted are in list then why we are passing some different object and comparing with it as it is not in list and will not be included in our result also. How Sorting is taking place here?

4) What different values we can pass for second parameter in line 5 ?

Would be appreciable if make each query understandable.

In line 3 Collections.sort takes a list as parameter and compareTo(object o) is executed by the objects of the list taking as argument other objects of the list.

In line 2 name is the atribute name of the object who executes the method.

When you call collections.sort(lt) compareTo(Object o) is used to do the orderig and is executed by several objects of the list taking other objects of the list as arguments, it depends of the shorting algoritm that collections.sort() uses.

For example in a list l with only two objects calling collections.sort(l) will make compareTo(Object o) be executed by one object taking the other as argument and the list will be in order. Bigger list will require more calls.

collections.sort() acepts one parameter a list or a list and a Comparator.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM