Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Comparable5 {
- public static void main(String[] args) {
- TreeSet ts=new TreeSet();
- ts.add(new Student1(2,"Thilan"));
- ts.add(new Student1(1,"Kamal"));
- ts.add(new Student1(3,"Nimal"));
- System.out.println(ts);
- }
- }
- class Student1 implements Comparable<Student>{
- int id;
- String name;
- public Student1(int id,String name){
- this.id=id;
- this.name=name;
- }
- @Override
- public String toString() {
- return this.id+":"+this.name;
- }
- @Override
- public int compareTo(Student o) {
- if (o.name.equals(this.name)) {
- return -1;
- } else if(o.name.equals(this.name)){
- return +1;
- }
- else{
- return 0;
- }
- }
- }
Add Comment
Please, Sign In to add comment