suppose, I have a student class with roll number and name. I want to sort it out wrt roll number. I tried the following .Here is my code:

```
package CollectionDemo;
import java.util.*;

class student1 implements Comparable<student1>{
    int rollNo;
    String name;

    student1(int rollNo,String name){
        this.rollNo=rollNo;
        this.name=name;
    }
    @Override
    public boolean equals(Object o){
        if((o instanceof student1) && (((student1)o).rollNo == rollNo)){
             return true;
        }
          else
        {
              return false;
        }
    }

    @Override
    public int hashCode(){
        return 1;
    }

    public int compareTo(student1 s) {
        return s.rollNo;
    }

    public String toString(){
        return "["+rollNo+","+name+"]";
    }
}
public class treeSetDemo {
    public static void main(String... a){

        Set<student1> set=new TreeSet<student1>();
        set.add(new student1(102,"Anu"));
        set.add(new student1(101,"Tanu"));
        set.add(new student1(103,"Minu"));

        System.out.println("elements:"+set);

    }
}

o/p: elements:[[102,Anu], [101,Tanu], [103,Minu]]

```

so, its not sorting:( how to make it correct .

thanks for your help.

================================================

thanks for all your help. The following code runs fine, but now I want to know how it works, if i comment out equals and hashcode method.

```
package CollectionDemo;
import java.util.*;

class student1 implements Comparable<student1>{
    int rollNo;
    String name;

    student1(int rollNo,String name){
        this.rollNo=rollNo;
        this.name=name;
    }
   /* @Override
    public boolean equals(Object o){
        if((o instanceof student1) && (((student1)o).rollNo == rollNo)){
             return true;
        }
          else
        {
              return false;
        }
    }

    @Override
    public int hashCode(){
        return 1;
    }
*/
    public int compareTo(student1 s) {
        System.out.println("hello:"+(this.rollNo-s.rollNo));
        return this.rollNo-s.rollNo;
    }

    public String toString(){
        return "["+rollNo+","+name+"]";
    }
}
public class treeSetDemo {
    public static void main(String... a){

        Set<student1> set=new TreeSet<student1>();
        set.add(new student1(102,"Anu"));
        set.add(new student1(101,"Tanu"));
        set.add(new student1(103,"Minu"));

        System.out.println("elements:"+set);

    }
}

```

OP:
run:
hello:-1
hello:1
elements:[[101,Tanu], [102,Anu], [103,Minu]]
BUILD SUCCESSFUL (total time: 0 seconds)