TreeSet是Set集合的常見子類.
TreeSet:底層結(jié)構(gòu)是 二叉樹
元素是有排序的,但是不可以有重復(fù)元素.
相關(guān)代碼演練:
/*TreeSet ;元素是有序的,但是不可以元素重復(fù). */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的兩種排序方式:第一種排序方式:(讓元素自身具備比較性)讓該類實(shí)現(xiàn)Comparable接口.并且覆寫compareTo方法.這種排序是自然排序.
保證元素唯一性的依據(jù)是: compareTo方法return 0;
代碼演示:
/* TreeSet存儲(chǔ)自定義對(duì)象 (會(huì)拋出類型轉(zhuǎn)換異常ClassCastException) Student類無法轉(zhuǎn)成Comparable. 因此Student類需要實(shí)現(xiàn)Comparable接口.并且覆寫compareTo方法.排序的方式為自然順序. 排序時(shí),當(dāng)主要條件相同時(shí),還要判斷一下次要條件. 需求: 按學(xué)生的年齡排序.*/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 //該接口強(qiáng)制讓學(xué)生具備了比較性. (實(shí)現(xiàn)了該接口需要覆蓋其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; //倒取存入的對(duì)象 //return 0; //只會(huì)存入第一個(gè)存入的對(duì)象. if(!(obj instanceof Student)) throw new RuntimeException("不是學(xué)生對(duì)象"); 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; }}
第二種排序方式:(元素不具備比較性,或者比較性是所不需要的)此時(shí),讓集合自身具備比較性.在集合一初始化時(shí),就有比較方式.實(shí)現(xiàn)方式: 定義一個(gè)類,實(shí)現(xiàn)comparator接口,并覆寫其中的compare方法.
/*讓容器自身具備比較性,定義一個(gè)比較器.將比較器對(duì)象傳遞給TreeSet集合的構(gòu)造函數(shù);思路步驟:定義一個(gè)容器讓這個(gè)容器實(shí)現(xiàn)comparator,并且覆寫其中的compare方法.*/import java.util.*;class TreeSetTest2{ public static void main(String [] args) { //TreeSet ts = new TreeSet(); //傳入的不是比較器對(duì)象時(shí),調(diào)用的是CompareTo方法. TreeSet ts = new TreeSet(new MyComparator()); //將比較器傳遞給TreeSet的構(gòu)造函數(shù). 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 //讓學(xué)生類具備了比較性{ 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 //定義一個(gè)容器并實(shí)現(xiàn)了Comparator接口{ public int compare(Object o1,Object o2) //覆寫其中的compare方法. { if(!(o1 instanceof Student ||o2 instanceof Student)) throw new RuntimeException("對(duì)象錯(cuò)誤."); Student s1 = (Student)o1; Student s2 = (Student)o2; //return s1.getName().compareTo(s2.getName()); //無法存儲(chǔ)到姓名相同年齡不同的對(duì)象. int num = s1.getName().compareTo(s2.getName()); //判斷返回值 (1 0 -1) if(num==0) //判斷的名字相同時(shí),在判斷其年齡是否相同. { return s1.getAge()-s2.getAge(); /* if(s1.getAge()>s2.getAge()) return 1; if(s1.getAge()==s2.getAge()) return 0; return -1; */ } return num; }}
練習(xí):
按照字符串的長(zhǎng)度進(jìn)行排序.
/*練習(xí):按照字符串長(zhǎng)度排序*/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) //判斷長(zhǎng)度一樣的字符串的自然排序. { return s1.compareTo(s2); } return num; }}
注意:一定要記得先對(duì)主要條件進(jìn)行判斷,在對(duì)次要條件進(jìn)行判斷.避免出現(xiàn)漏存的情況.
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注