Treeset的两种排序方法

来源:互联网 发布:网络写作软件哪个好 编辑:程序博客网 时间:2024/06/03 18:56
[java] view plain copy
  1. 转自:<a href="http://luochuang724.blog.163.com/blog/static/203606212201222821045518/">http://luochuang724.blog.163.com/blog/static/203606212201222821045518/</a>  
[java] view plain copy
  1. Treeset 的自定义的两种排序方式  
  2.   
  3. 第一种:在元素中定义排序规则。元素自身具有比较性实现Comparable接口 覆盖compareTo方法  
  4. import java.util.Iterator;  
  5. import java.util.TreeSet;  
  6. /*** 
  7.  *TreeSet是一个有序集合,TreeSet中元素将按照升序排列,缺省是按照 
  8.   自然顺序进行排列,意味着TreeSet中元素要实现Comparable接口。 
  9.   我们可以在构造TreeSet对象时,传递实现了Comparator接口的比较器对象。 
  10.   注意排序時:當主要的條件相同時,判斷次要條件。 
  11.  * @author Administrator 
  12.  * 
  13.  */  
  14. public class TreeSetTest {  
  15.  public static void main(String[] args) {  
  16.   TreeSet treeset = new TreeSet();//定义一个集合  
  17.   treeset.add(new person2(10"liuyia"));  
  18.   treeset.add(new person2(10"liuyib"));  
  19.   treeset.add(new person2(15"liuyi34"));  
  20.   treeset.add(new person2(11"liuyi4"));  
  21.   treeset.add(new person2(12"liuyi4"));  
  22.   
  23.   Iterator itera = treeset.iterator();  
  24.   while (itera.hasNext()) {  
  25.    System.out.println(itera.next());  
  26.   }  
  27.   
  28.  }  
  29. }  
  30.   
  31. class person2 implements Comparable {//实现Comparable 接口  private int age;  
  32.   
  33.  private String name;  
  34.   
  35.  public int getAge() {  
  36.   return age;  
  37.  }  
  38.   
  39.  public void setAge(int age) {  
  40.   this.age = age;  
  41.  }  
  42.   
  43.  public String getName() {  
  44.   return name;  
  45.  }  
  46.   
  47.  public void setName(String name) {  
  48.   this.name = name;  
  49.  }  
  50.   
  51.  public person2(int age, String name) {  
  52.   this.age = age;  
  53.   this.name = name;  
  54.     
  55.     
  56.    
  57.  }  
  58.   
  59.   
  60.  public int compareTo(Object o) {  
  61.   if(!(o instanceof person2))  
  62.    throw new RuntimeException("對象不對哇!!");  
  63.    person2 p = (person2)o;  
  64.    if(this.age>p.age)  
  65.    {  
  66.     return -1;  
  67.    }  
  68.    if(this.age<p.age)  
  69.    {  
  70.     return 1;  
  71.    }  
  72.      
  73.    if(this.age==p.age)  
  74.    {   
  75.     return this.name.compareTo(p.name);//當主要的條件也就是age的值相同時時候此時判斷次要條件姓名  
  76.    }  
  77.      
  78.   
  79.   return -1;  
  80.     
  81.  }  
  82.  //用於設置打印結果  
  83.  public String toString()  
  84.  {  
  85.   return age+" = "+"name"+name;  
  86.  }  
  87. }  
  88.   
  89. 第二种:在集合中定义排序  实现Comparator接口 覆盖compare方法。  
  90.   
  91. TreeSet(Comparator<? super E> comparator)   
  92.           构造一个新的空 TreeSet,它根据指定比较器进行排序。  
  93.   
  94. import java.util.Comparator;  
  95. import java.util.Iterator;  
  96. import java.util.TreeSet;  
  97.   
  98. public class TreeSetTest {  
  99.  public static void main(String[] args) {  
  100.   TreeSet treeset = new TreeSet( new mycomp());//定义一个集合  
  101.   treeset.add(new person2(10"liuyia"));  
  102.   treeset.add(new person2(10"liuyib"));  
  103.   treeset.add(new person2(15"liuyi34"));  
  104.   treeset.add(new person2(11"liuyi4"));  
  105.   treeset.add(new person2(12"liuyi4"));  
  106.   
  107.   Iterator itera = treeset.iterator();  
  108.   while (itera.hasNext()) {  
  109.    System.out.println(itera.next());  
  110.   }  
  111.   
  112.  }  
  113. }  
  114.   
  115. class person2 {  
  116.  private int age;  
  117.   
  118.  private String name;  
  119.   
  120.  public int getAge() {  
  121.   return age;  
  122.  }  
  123.   
  124.  public void setAge(int age) {  
  125.   this.age = age;  
  126.  }  
  127.   
  128.  public String getName() {  
  129.   return name;  
  130.  }  
  131.   
  132.  public void setName(String name) {  
  133.   this.name = name;  
  134.  }  
  135.   
  136.  public person2(int age, String name) {  
  137.   this.age = age;  
  138.   this.name = name;  
  139.  }  
  140.  public String toString()  
  141.  {  
  142.   return age+" = "+"name"+name;  
  143.  }  
  144. }  
  145.   
  146.   
  147. class mycomp implements Comparator  
  148. {  
  149.   
  150.  public int compare(Object o1, Object o2) {  
  151.   person2 p1 = (person2)o1;  
  152.   person2 p2 = (person2)o2;  
  153.   return -(p1.getAge()- p2.getAge());  
  154.  }  
  155. }  
0 0
原创粉丝点击