java TreeSet的使用

java TreeSet的使用
2011-06-07 19:34

/*
 
TreeSet:它可以給Set集合中的元素進行指定方式的排序。
        保證元素唯一性的方式:通過比較的結果是否爲0.
        底層數據結構是:二叉樹。
*/
 
 
import java.util.*;
 
 
class TreeSetDemo2  
{
    public static void main(String[] args)  
    {
        TreeSet ts = new TreeSet();
 
        ts.add(new Student("lisi0",30));
        ts.add(new Student("lisixx",29));
        ts.add(new Student("lisi9",29));
        ts.add(new Student("lisi8",38));
        ts.add(new Student("lisixx",29));
        ts.add(new Student("lisi4",14));
        //ts.add(new Student(39));
        ts.add(new Student("lisi7",27));
 
 
 
        System.out.println(ts);
    }
}
 
//同姓名同年齡的學生視爲同一個學生。按照學生的年齡排序。
class Student implements Comparable
{
    private int age;
    private String name;
    Student(String name,int age)
    {
        this.age = age;
        this.name = name;
    }
 
    public int compareTo(Object obj)
    {
        
        Student stu = (Student)obj;
        
        int num = new Integer(this.age).compareTo(new Integer(stu.age));
 
        return num==0?this.name.compareTo(stu.name):num;
 
        /*
        if(this.age>stu.age)
            return 1;
        if(this.age==stu.age)
            return this.name.compareTo(stu.name);
        return -1;
        */
        /**/
    }
 
    public int getAge()
    {
        return age;
    }
    public String toString()
    {
        return name+"::"+age;
    }
}

 

=============================================


/*
 
TreeSet:它可以給Set集合中的元素進行指定方式的排序。
        保證元素唯一性的方式:通過比較的結果是否爲0.
        底層數據結構是:二叉樹。
 
        排序的第一種方式:
            讓元素自身具備比較性。只要讓元素實現Comparable接口,覆蓋compareTo方法即可。
            
            但是,如果元素自身不具備比較性,或者元素自身具備的比較性,不是所需要的。
            比如,學生的自然排序是按年齡排序,現在想要按照學生姓名排序。還可以不改動原有代碼。
            這時怎麼辦呢?
        排序的第二種方式:自定比較器的方式。
            這時可以讓集合自身具備比較性。
            可以定義一個類實現Comparator接口,覆蓋compare方法。將該Comparator接口子類對象作爲實際參數
            傳遞給TreeSet集合構造函數。
 
            該對象就是比較器。
 
 
 
*/
 
 
import java.util.*;
 
 
class TreeSetDemo3  
{
    public static void main(String[] args)  
    {
        TreeSet ts = new TreeSet(new StudentComparatorByName());
 
        ts.add(new Student("lisi0",30));
        ts.add(new Student("lisixx",29));
        ts.add(new Student("lisi9",29));
        ts.add(new Student("lisi8",38));
        ts.add(new Student("lisixx",29));
        ts.add(new Student("lisi4",14));
        //ts.add(new Student(39));
        ts.add(new Student("lisi7",27));
 
 
 
        System.out.println(ts);
    }
}
 
class StudentComparatorByName implements Comparator
{
    public int compare(Object o1,Object o2)
    {
        Student s1 = (Student)o1;
        Student s2 = (Student)o2;
 
        int num = s1.getName().compareTo(s2.getName());
        return num==0?new Integer(s1.getAge()).compareTo(new Integer(s2.getAge())):num;
    }
}
 
 
//同姓名同年齡的學生視爲同一個學生。按照學生的年齡排序。
class Student implements Comparable
{
    private int age;
    private String name;
    Student(String name,int age)
    {
        this.age = age;
        this.name = name;
    }
 
    public int compareTo(Object obj)
    {
        
        Student stu = (Student)obj;
        
        int num = new Integer(this.age).compareTo(new Integer(stu.age));
 
        return num==0?this.name.compareTo(stu.name):num;
 
        /*
        if(this.age>stu.age)
            return 1;
        if(this.age==stu.age)
            return this.name.compareTo(stu.name);
        return -1;
        */
        /**/
    }
 
    public int getAge()
    {
        return age;
    }
    public String getName()
    {
        return name;
    }
    public String toString()
    {
        return name+"::"+age;
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章