TreeSet是Set集合的常見子類.
TreeSet:底層結構是 二叉樹
元素是有排序的,但是不可以有重復元素.
相關代碼演練:
/*TreeSet ;元素是有序的,但是不可以元素重復. */import java.util.*;class TreeSetDemo{ public static void main(String [] args) { TreeSet ts = new TreeSet(); ts.add("java01"); ts.add("java03"); ts.add("java02"); ts.add("java04"); ts.add("java04"); Iterator it = ts.iterator(); while(it.hasNext()) { sop(it.next()); } ts.remove("java02"); sop(ts); } public static void sop(Object obj) { System.out.PRintln(obj); } }class Student{ private String name; private int age; Student(String name,int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; }}
TreeSet的兩種排序方式:第一種排序方式:(讓元素自身具備比較性)讓該類實現Comparable接口.并且覆寫compareTo方法.這種排序是自然排序.
保證元素唯一性的依據是: compareTo方法return 0;
代碼演示:
/* TreeSet存儲自定義對象 (會拋出類型轉換異常ClassCastException) Student類無法轉成Comparable. 因此Student類需要實現Comparable接口.并且覆寫compareTo方法.排序的方式為自然順序. 排序時,當主要條件相同時,還要判斷一下次要條件. 需求: 按學生的年齡排序.*/import java.util.*;class TreeSetTest{ public static void main(String [] args) { TreeSet ts = new TreeSet(); ts.add(new Student("lisi08",19)); ts.add(new Student("lisi02",22)); ts.add(new Student("lisi007",20)); ts.add(new Student("lisi09",19)); Iterator it = ts.iterator(); while(it.hasNext()) { Student stu = (Student)it.next(); sop(stu.getName()+"------"+stu.getAge()); } } public static void sop(Object obj) { System.out.println(obj); } }class Student implements Comparable //該接口強制讓學生具備了比較性. (實現了該接口需要覆蓋其compareTo方法){ private String name; private int age; Student(String name,int age) { this.name = name; this.age = age; } public int compareTo(Object obj) { //return 1; //怎么存的怎么取出 //return -1; //倒取存入的對象 //return 0; //只會存入第一個存入的對象. if(!(obj instanceof Student)) throw new RuntimeException("不是學生對象"); Student s = (Student)obj; System.out.println(this.name+"---compareTo---"+s.name); if(this.age>s.age) return 1; if(this.age==s.age) { return this.name.compareTo(s.name); } return -1; } public String getName() { return name; } public int getAge() { return age; }}
第二種排序方式:(元素不具備比較性,或者比較性是所不需要的)此時,讓集合自身具備比較性.在集合一初始化時,就有比較方式.實現方式: 定義一個類,實現comparator接口,并覆寫其中的compare方法.
/*讓容器自身具備比較性,定義一個比較器.將比較器對象傳遞給TreeSet集合的構造函數;思路步驟:定義一個容器讓這個容器實現comparator,并且覆寫其中的compare方法.*/import java.util.*;class TreeSetTest2{ public static void main(String [] args) { //TreeSet ts = new TreeSet(); //傳入的不是比較器對象時,調用的是CompareTo方法. TreeSet ts = new TreeSet(new MyComparator()); //將比較器傳遞給TreeSet的構造函數. ts.add(new Student("lisi08",19)); ts.add(new Student("lisi02",22)); ts.add(new Student("lisi02",23)); ts.add(new Student("lisi007",20)); ts.add(new Student("lisi09",19)); Iterator it = ts.iterator(); while(it.hasNext()) { Student stu = (Student)it.next(); sop(stu.getName()+"-----"+stu.getAge()); } } public static void sop(Object obj) { System.out.println(obj); } }class Student implements Comparable //讓學生類具備了比較性{ private String name; private int age; Student(String name,int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } public int compareTo(Object obj) //覆寫了Comparable中的compareTo方法. { if(!(obj instanceof Student)) throw new RuntimeException("bushixueshengduixiang"); Student s = (Student)obj; if(this.age>s.age) return 1; if(this.age==s.age) { return this.name.compareTo(s.name); } return -1; }}class MyComparator implements Comparator //定義一個容器并實現了Comparator接口{ public int compare(Object o1,Object o2) //覆寫其中的compare方法. { if(!(o1 instanceof Student ||o2 instanceof Student)) throw new RuntimeException("對象錯誤."); Student s1 = (Student)o1; Student s2 = (Student)o2; //return s1.getName().compareTo(s2.getName()); //無法存儲到姓名相同年齡不同的對象. int num = s1.getName().compareTo(s2.getName()); //判斷返回值 (1 0 -1) if(num==0) //判斷的名字相同時,在判斷其年齡是否相同. { return s1.getAge()-s2.getAge(); /* if(s1.getAge()>s2.getAge()) return 1; if(s1.getAge()==s2.getAge()) return 0; return -1; */ } return num; }}
練習:
按照字符串的長度進行排序.
/*練習:按照字符串長度排序*/import java.util.*;class TreeSetExam{ public static void main(String [] args) { TreeSet ts = new TreeSet(new StrLengthCompare()); ts.add("abcd"); ts.add("cc"); ts.add("cba"); ts.add("a"); ts.add("hahaha"); ts.add("aa"); Iterator it = ts.iterator(); while(it.hasNext()) { sop(it.next()); } } public static void sop(Object obj) { System.out.println(obj); } }class StrLengthCompare implements Comparator{ public int compare(Object o1,Object o2) { if(!(o1 instanceof String || o2 instanceof String)) throw new RuntimeException("ERROR"); String s1 = (String)o1; String s2 = (String)o2; //int num = s1.length()-s2.length(); int num = new Integer(s1.length()).compareTo(new Integer(s2.length())); if(num==0) //判斷長度一樣的字符串的自然排序. { return s1.compareTo(s2); } return num; }}
注意:一定要記得先對主要條件進行判斷,在對次要條件進行判斷.避免出現漏存的情況.
新聞熱點
疑難解答