1.Treeset中的集合石有序的,(有序即是按照大小写排序),是用二叉树实现的.
2.明白二叉树的数据结构.
遍历的时候 前序: 中左右
中序: 左中右
后序: 左右中
3.TreeSet 是 无序 不允许重复的,无序: 是展示顺序与存入顺序不同
但是是可排序的,是可以按照大小顺序排列的
4.排序可分为:自然排序 1234 abcdef 包括自定义的类
定制排序 1234 abcdef 包括自定义的类
题目要求:有很多人 :
每一个人 有一本书
先按照 人的 年龄降序排序
按照 人的姓名升序排序
按照书的价格降序排序
按照书的 书名升序排序
首先看自然排序:
Person类中有name age book对象属性.Person类要实现Comparable接口,后面<>里面就写Person类.另外要注意,Person类要重写compareTo()方法.在此方法里面,如果比较带有汉字的字符串的值,不能直接用String进行比较.这就要求先创建一个Colator对象,这个对象进行调用getColllationKey("汉字") 来创建一个CollationKey对象.
CollationKey对象调用compareTo方法可以返回比较值.即num,看代码:
Collator collator = Collator.getInstance();
CollationKey key = collator.getCollationKey(this.name);
CollationKey key2 = collator.getCollationKey(o.name);
int num = key.compareTo(key2);
另外,在姓名相同比较书的属性值得时候,会有一个低耦合,高内聚的知识点.
1.耦合性:也称块间联系。指软件系统结构中各模块间相互联系紧密程度的一种度量。模块之间联系越紧密,其耦合性就越强,模块的独立性则越差。模块间耦合高低取决于模块间接口的复杂性、调用的方式及传递的信息.
2.内聚性:又称块内联系。指模块的功能强度的度量,即一个模块内部各个元素彼此结合的紧密程度的度量。若一个模块内各元素(语名之间、程序段之间)联系的越紧密,则它的内聚性就越高。
所以,如果把比较书的属性值最好放在其本类进行比较,为的是如果修改某属性名或者方法名,牵扯到的一些需要修改的地方就只是在本类修改.在Book类中,因为要比较书的属性值,所以要实现Comparable接口,在重写的compareTo()方法中进行比较.
package com.qf.demo6;
// 低耦合 高内聚
import java.text.CollationKey;
import java.text.Collator;
public class Person implements Comparable{
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
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{
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
return -1;
}else{
return 0;
}