Guest User

Untitled

a guest
Jun 20th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. public class Comparable5 {
  2. public static void main(String[] args) {
  3. TreeSet ts=new TreeSet();
  4. ts.add(new Student1(2,"Thilan"));
  5. ts.add(new Student1(1,"Kamal"));
  6. ts.add(new Student1(3,"Nimal"));
  7.  
  8. System.out.println(ts);
  9. }
  10. }
  11. class Student1 implements Comparable<Student>{
  12. int id;
  13. String name;
  14. public Student1(int id,String name){
  15. this.id=id;
  16. this.name=name;
  17. }
  18.  
  19.  
  20.  
  21. @Override
  22. public String toString() {
  23. return this.id+":"+this.name;
  24. }
  25.  
  26. @Override
  27. public int compareTo(Student o) {
  28. if (o.name.equals(this.name)) {
  29. return -1;
  30. } else if(o.name.equals(this.name)){
  31. return +1;
  32. }
  33. else{
  34. return 0;
  35. }
  36. }
  37.  
  38. }
Add Comment
Please, Sign In to add comment