1.Treeset中的集合石有序的,(有序即是按照大小寫排序),是用二叉樹實現(xiàn)的.
2.明白二叉樹的數(shù)據(jù)結(jié)構(gòu).
遍歷的時候 前序: 中左右
中序: 左中右
后序: 左右中
3.TreeSet 是 無序 不允許重復(fù)的,無序: 是展示順序與存入順序不同
但是是可排序的,是可以按照大小順序排列的
4.排序可分為:自然排序 1234 abcdef 包括自定義的類
定制排序 1234 abcdef 包括自定義的類
題目要求:有很多人 :
- 每一個人 有一本書
- 先按照 人的 年齡降序排序
- 按照 人的姓名升序排序
- 按照書的價格降序排序
- 按照書的 書名升序排序
首先看自然排序:
Person類中有name age book對象屬性.Person類要實現(xiàn)Comparable接口,后面<>里面就寫Person類.另外要注意,Person類要重寫compareTo()方法.在此方法里面,如果比較帶有漢字的字符串的值,不能直接用String進行比較.這就要求先創(chuàng)建一個Colator對象,這個對象進行調(diào)用getColllationKey("漢字") 來創(chuàng)建一個CollationKey對象.
CollationKey對象調(diào)用compareTo方法可以返回比較值.即num,看代碼:
Collator collator = Collator.getInstance();
CollationKey key = collator.getCollationKey(this.name);
CollationKey key2 = collator.getCollationKey(o.name);
int num = key.compareTo(key2);
另外,在姓名相同比較書的屬性值得時候,會有一個低耦合,高內(nèi)聚的知識點.
1.耦合性:也稱塊間聯(lián)系吩蔑。指軟件系統(tǒng)結(jié)構(gòu)中各模塊間相互聯(lián)系緊密程度的一種度量。模塊之間聯(lián)系越緊密蔚万,其耦合性就越強珠插,模塊的獨立性則越差闸准。模塊間耦合高低取決于模塊間接口的復(fù)雜性版确、調(diào)用的方式及傳遞的信息.
2.內(nèi)聚性:又稱塊內(nèi)聯(lián)系。指模塊的功能強度的度量尚洽,即一個模塊內(nèi)部各個元素彼此結(jié)合的緊密程度的度量悔橄。若一個模塊內(nèi)各元素(語名之間、程序段之間)聯(lián)系的越緊密腺毫,則它的內(nèi)聚性就越高癣疟。
所以,如果把比較書的屬性值最好放在其本類進行比較,為的是如果修改某屬性名或者方法名,牽扯到的一些需要修改的地方就只是在本類修改.在Book類中,因為要比較書的屬性值,所以要實現(xiàn)Comparable接口,在重寫的compareTo()方法中進行比較.
package com.qf.demo6;
// 低耦合 高內(nèi)聚
import java.text.CollationKey;
import java.text.Collator;
public class Person implements Comparable<Person>{
private String name;
private int age;
private Book book;
public Person(String name, int age, Book book) {
super();
this.name = name;
this.age = age;
this.book = book;
}
public Person() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", book=" + book + "]";
}
@Override
//按照年齡 降序
public int compareTo(Person o) {
if(this.age>o.age){
return -1;
}else if(this.age<o.age){
return 1;
}else {//年齡相同 比較姓名 升序
// 第二排序
Collator collator = Collator.getInstance();
CollationKey key = collator.getCollationKey(this.name);
CollationKey key2 = collator.getCollationKey(o.name);
int num = key.compareTo(key2);
if(num>0){
return 1;
}else if(num<0){
return -1;
}else {//姓名相同 比較書的一些屬性值
// 第三條件 和第四條件
return this.book.compareTo(o.book);
}
}
}
}
package com.qf.demo6;
import java.text.CollationKey;
import java.text.Collator;
public class Book implements Comparable<Book>{
private String bookName;
private int price;
public Book(String bookName, int price) {
super();
this.bookName = bookName;
this.price = price;
}
public Book() {
super();
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public String toString() {
return "Book [bookName=" + bookName + ", price=" + price + "]";
}
@Override
public int compareTo(Book o) {
// 第三條件 降序
if(this.getPrice()>o.getPrice()){
return -1;
}else if(this.getPrice()< o.getPrice()){
return 1;
}else{
// 第四條件 升序
Collator collator2 = Collator.getInstance();
CollationKey key3 = collator2.getCollationKey(this.getBookName());
CollationKey key4 = collator2.getCollationKey(o.getBookName());
int num1 = key3.compareTo(key4);
return num1;
}
}
}
// 如果this.age大于o.age 返回1的話 就是把this對象往后放,即是升序.
if(this.age>o.age){
return 1;
}else if(this.age<o.age){
return -1;
}else{
return 0;
}