TreeSet集合排序两种实现方式Comparable和Comparator比较

  1. import java.util.Comparator;  
  2. import java.util.Iterator;  
  3. import java.util.TreeSet;  
  4.   
  5. /** 
  6.  * TreeSet集合排序有两种方式,Comparable和Comparator区别: 
  7.  * 1:让元素自身具备比较性,需要元素对象实现Comparable接口,覆盖compareTo方法。 
  8.  * 2:让集合自身具备比较性,需要定义一个实现了Comparator接口的比较器,并覆盖compare方法, 
  9.  *   并将该类对象作为实际参数传递给TreeSet集合的构造函数。 第二种方式较为灵活。 
  10.  */  
  11. public class TreeSetDemo3 {  
  12.     public static void main(String[] args) {  
  13.           
  14.         Teacher t1 = new Teacher(111L);  
  15.         Teacher t2 = new Teacher(222L);  
  16.           
  17.         TreeSet<Teacher> teachers = new TreeSet<Teacher>();  
  18.         teachers.add(t1);  
  19.         teachers.add(t2);  
  20.           
  21.         for (Iterator<Teacher> iter = teachers.iterator(); iter.hasNext();) {  
  22.             Teacher t =  iter.next();  
  23.             System.out.println(t.getId());  
  24.         }  
  25.           
  26.         System.out.println("------------------------");  
  27.           
  28.         Student s1 = new Student(123L);  
  29.         Student s2 = new Student(456L);                 //让集合自身具备比较性,这种方式较灵活      
  30.         TreeSet<Student> students = new TreeSet<Student>(new StudentComparator());    
  31.   
  32.         students.add(s1);  
  33.         students.add(s2);  
  34.   
  35.         for (Iterator<Student> iter = students.iterator(); iter.hasNext();) {  
  36.             Student s = iter.next();  
  37.             System.out.println(s.getId());  
  38.         }  
  39.           
  40.     }  
  41. }  
  42.   
  43. /** 
  44.  * 采用实现Comparable接口重写compareTo方法的方式,让元素自身具备比较性 
  45.  * @author Administrator 
  46.  * 
  47.  */  
  48. class Teacher implements Comparable<Teacher>{  
  49.     private Long id;  
  50.       
  51.     public Teacher() {  
  52.     }  
  53.     public Teacher(Long id) {  
  54.         this.id = id;  
  55.     }  
  56.   
  57.     public Long getId() {  
  58.         return id;  
  59.     }  
  60.     public void setId(Long id) {  
  61.         this.id = id;  
  62.     }  
  63.       
  64.     @Override  
  65.     public int compareTo(Teacher o) {  
  66.         return (int) (this.id - o.getId());  
  67.     }  
  68. }  
  69.   
  70. //学生类  
  71. class Student {  
  72.   
  73.     private Long id;  
  74.   
  75.     public Student() {  
  76.     }  
  77.     public Student(Long id) {  
  78.         this.id = id;  
  79.     }  
  80.   
  81.     public Long getId() {  
  82.         return id;  
  83.     }  
  84.     public void setId(Long id) {  
  85.         this.id = id;  
  86.     }  
  87. }  
  88.   
  89. /** 
  90.  * 自定义定义一个实现了Comparator接口的比较器,并覆盖compare方法 
  91.  */  
  92. class StudentComparator implements Comparator<Student> {  
  93.     @Override  
  94.     public int compare(Student o1, Student o2) {  
  95.         return (int) (o1.getId() - o2.getId()); // 根据升序排序  
  96.     }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值