java之compareto方法的介紹與TreeSet的使用



java中的compareto方法的詳細介紹
javacompareTo 
java中的compareto方法,返回參與比較的前后兩個字符串的asc碼的差值,看下面一組代碼
String a="a",b="b";
System.out.println(a.compareto.b);
則輸出-1;若a="a",b="a"則輸出0;若a="b",b="a"則輸出1; 
單個字符這樣比較,若字符串比較長呢??
若a="ab",b="b",則輸出-1;
若a="abcdef",b="b"則輸出-1;也就是說,如果兩個字符串首字母不同,則該方法返回首字母的asc碼的差值; 

如果首字母相同呢??
若a="ab",b="a",輸出1;
若a="abcdef",b="a"輸出5;
若a="abcdef",b="abc"輸出3;
若a="abcdef",b="ace"輸出-1;
即參與比較的兩個字符串如果首字符相同,則比較下一個字符,直到有不同的為止,返回該不同的字符的asc碼差值,如果兩個字符串不一樣長,可以參與比較的字符又完全一樣,則返回兩個字符串的長度差值 


分兩種情況:1、如果這兩個字符串不同,compareTo返回這兩個字符串在位置k處兩個char值的差,即值為this.charAt(k)-anotherString.charAt(k)。
      2、如果沒有字符不同的索引位置,則較短字符串的字典順序在較長字符串之前。在這種情況下,
compareTo返回這兩個字符串長度的差,即值為this.length() -anotherString.length()。

 

 1 /*
 2  
 3 TreeSet:它可以給Set集合中的元素進行指定方式的排序。
 4         保證元素唯一性的方式:通過比較的結果是否為0.
 5         底層數據結構是:二叉樹。
 6 */
 7  
 8  
 9 import java.util.*;
10  
11  
12 class TreeSetDemo2  
13 {
14     public static void main(String[] args)  
15     {
16         TreeSet ts = new TreeSet();
17  
18         ts.add(new Student("lisi0",30));
19         ts.add(new Student("lisixx",29));
20         ts.add(new Student("lisi9",29));
21         ts.add(new Student("lisi8",38));
22         ts.add(new Student("lisixx",29));
23         ts.add(new Student("lisi4",14));
24         //ts.add(new Student(39));
25         ts.add(new Student("lisi7",27));
26  
27         System.out.println(ts);
28     }
29 }
30  
31 //同姓名同年齡的學生視為同一個學生。按照學生的年齡排序。
32 class Student implements Comparable
33 {
34     private int age;
35     private String name;
36     Student(String name,int age)
37     {
38         this.age = age;
39         this.name = name;
40     }
41  
42     public int compareTo(Object obj)
43     {
44         
45         Student stu = (Student)obj;
46         
47         int num = new Integer(this.age).compareTo(new Integer(stu.age));
48  
49         return num==0?this.name.compareTo(stu.name):num;
50  
51         /*
52         if(this.age>stu.age)
53             return 1;
54         if(this.age==stu.age)
55             return this.name.compareTo(stu.name);
56         return -1;
57         */
58         /**/
59     }
60  
61     public int getAge()
62     {
63         return age;
64     }
65     public String toString()
66     {
67         return name+"::"+age;
68     }
69 }
70  

 

  1 /*
  2   
  3  TreeSet:它可以給Set集合中的元素進行指定方式的排序。
  4          保證元素唯一性的方式:通過比較的結果是否為0.
  5          底層數據結構是:二叉樹。
  6   
  7          排序的第一種方式:
  8              讓元素自身具備比較性。只要讓元素實現Comparable接口,覆蓋compareTo方法即可。
  9              
 10              但是,如果元素自身不具備比較性,或者元素自身具備的比較性,不是所需要的。
 11              比如,學生的自然排序是按年齡排序,現在想要按照學生姓名排序。還可以不改動原有代碼。
 12              這時怎么辦呢?
 13          排序的第二種方式:自定比較器的方式。
 14              這時可以讓集合自身具備比較性。
 15              可以定義一個類實現Comparator接口,覆蓋compare方法。將該Comparator接口子類對象作為實際參數
 16              傳遞給TreeSet集合構造函數。
 17   
 18              該對象就是比較器。
 19   
 20   
 21   
 22  */
 23   
 24   
 25  import java.util.*;
 26   
 27   
 28  class TreeSetDemo3  
 29  {
 30      public static void main(String[] args)  
 31      {
 32          TreeSet ts = new TreeSet(new StudentComparatorByName());
 33   
 34          ts.add(new Student("lisi0",30));
 35          ts.add(new Student("lisixx",29));
 36          ts.add(new Student("lisi9",29));
 37          ts.add(new Student("lisi8",38));
 38          ts.add(new Student("lisixx",29));
 39          ts.add(new Student("lisi4",14));
 40          //ts.add(new Student(39));
 41          ts.add(new Student("lisi7",27));
 42   
 43   
 44   
 45          System.out.println(ts);
 46      }
 47  }
 48   
 49  class StudentComparatorByName implements Comparator
 50  {
 51      public int compare(Object o1,Object o2)
 52      {
 53          Student s1 = (Student)o1;
 54          Student s2 = (Student)o2;
 55   
 56          int num = s1.getName().compareTo(s2.getName());
 57          return num==0?new Integer(s1.getAge()).compareTo(new Integer(s2.getAge())):num;
 58      }
 59  }
 60   
 61   
 62  //同姓名同年齡的學生視為同一個學生。按照學生的年齡排序。
 63  class Student implements Comparable
 64  {
 65      private int age;
 66      private String name;
 67      Student(String name,int age)
 68      {
 69          this.age = age;
 70          this.name = name;
 71      }
 72   
 73      public int compareTo(Object obj)
 74      {
 75          
 76          Student stu = (Student)obj;
 77          
 78          int num = new Integer(this.age).compareTo(new Integer(stu.age));
 79   
 80          return num==0?this.name.compareTo(stu.name):num;
 81   
 82          /*
 83          if(this.age>stu.age)
 84              return 1;
 85          if(this.age==stu.age)
 86              return this.name.compareTo(stu.name);
 87          return -1;
 88          */
 89          /**/
 90      }
 91   
 92      public int getAge()
 93      {
 94          return age;
 95      }
 96      public String getName()
 97      {
 98          return name;
 99      }
100      public String toString()
101      {
102          return name+"::"+age;
103      }
104  }

Integer 類型是一個對象,它也重寫了compareTo()方法。直接調用就可以。
int 類型是基本數據類型,如果要比較直接減就可以。

 

推薦:http://blog.csdn.net/you_off3/article/details/7465919

     http://itxiaohu.iteye.com/blog/1600313


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM