- TreeSet: * 基于 TreeMap 的 NavigableSet 实现。 * 1 使用元素的自然顺序对元素进行排序
使用空构造器 TreeSet ts= new TreeSet<>(); 2 或者根据创建 set 时提供的 Comparator 进行排序 * TreeSet ts= new
TreeSet<>(Comparator cp);
具体取决于使用的构造方法。 * 父接口:Set * 注意,此实现不是同步的。 * 从以下版本开始: 1.2 * 注意:不允许添加null元素 * * TreeSet的特点:去重且排序
- 对一组数字按照从小到大排序,并且去除重复元素 * 10,20,500,-60,8,41,22,36,20 * * 数组排序去重复 * 2个对象做比较结果为0,表示相等 * 2个对象做比较结果为-1,表示小于 *
2个对象做比较结果为1,表示大于 * * 大的往右走,小的往左走,相等不进来
import java.util.TreeSet;
public class Demo {
public static void main(String[] args) {
TreeSet<Integer> ts = new TreeSet<>();
ts.add(10);
ts.add(20);
ts.add(500);
ts.add(-60);
ts.add(8);
ts.add(41);
ts.add(22);
ts.add(36);
ts.add(20);
System.out.println(ts);
}
}
红黑树
- TreeSet:基于红黑树的二叉树(一种自平衡的二叉树)
* R-B Tree,全称是Red-Black Tree,又称为“红黑树”,它一种特殊的二叉查找树。红黑树的每个节点上都有存储位表示节点的颜色,可以是红(Red)或黑(Black)。
* 红黑树的特性:
* (1)每个节点或者是黑色,或者是红色。
* (2)根节点是黑色。
* (3)每个叶子节点(NIL)是黑色。 [注意:这里叶子节点,是指为空(NIL或NULL)的叶子节点!]
* (4)如果一个节点是红色的,则它的子节点必须是黑色的。
* (5)从一个节点到该节点的子孙节点的所有路径上包含相同数目的黑节点。
* 注意:
* (01) 特性(3)中的叶子节点,是只为空(NIL或null)的节点。
* (02) 特性(5),确保没有一条路径会比其他路径长出俩倍。因而,红黑树是相对是接近平衡的二叉树。
*
*
* 引用类型的数据放入TreeSet中,可以自动排序,而且去重
* TreeSet有2中排序方案
* 1:自然排序,创建无参的构造器 实现Comparable接口,并且重写CompareTo()方法
* 2:比较器排序,构造一个新的空 TreeSet,它根据指定比较器进行排序。 实现Compartor接口,重写compare方法
import java.util.TreeSet;
public class Demo2 {
/*
*/
public static void main(String[] args) {
TreeSet<Integer> ts = new TreeSet<>();
ts.add(2);
ts.add(1);
ts.add(3);
ts.add(4);
ts.add(5);
ts.add(7);
ts.add(6);
ts.add(6);
ts.add(8);
ts.add(9);
System.out.println(ts);
}
}
Demo
- TreeSet 自定义对象排序且去重
* 按照价格从小到大排序
import java.util.TreeSet;
public class Demo {
public static void main(String[] args) {
//1:创建一个set集合
TreeSet<Product> ts = new TreeSet<>();
//2:添加产品
ts.add(new Product("p1001", "电脑", 5000D, "武汉"));
ts.add(new Product("p1002", "键盘", 200D, "北京"));
ts.add(new Product("p1002", "键盘", 200D, "上海"));
ts.add(new Product("p1001", "电脑", 5000D, "武汉"));
//3:变量集合
System.out.println("编号\t名称\t价格\t出产地");
for (Product p : ts) {
System.out.println(p.getId()+"\t"+p.getName()+"\t"+p.getPrice()+
"\t"+p.getAddress());
}
}
}
注意方法的重写
public class Product implements Comparable<Product> {
private String id;
private String name;
private Double price;
private String address;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Product(String id, String name, Double price, String address) {
super();
this.id = id;
this.name = name;
this.price = price;
this.address = address;
}
public Product() {
super();
// TODO Auto-generated constructor stub
}
@Override
public int compareTo(Product p) {
//什么叫重复对象:所有属性的值完全相同,才成为同一个对象
//按照价格排序 主要条件
int num = (int)(this.getPrice()-p.getPrice());
//次要条件1 价格相同,编号不一定相同
int num1 = num==0?this.getId().compareTo(p.getId()):num;
//次要条件2 价格,编号相同,名称不一定相同
int num2 = num1==0?this.getName().compareTo(p.getName()):num1;
//次要条件3 价格,编号,名称相同,出产地不一定相同
int num3 = num2==0?this.getAddress().compareTo(p.getAddress()):num2;
return num3;
/*int num = (int)(this.getPrice()-p.getPrice());
//次要条件1 价格相同,编号不一定相同
num = num==0?this.getId().compareTo(p.getId()):num;
//次要条件2 价格,编号相同,名称不一定相同
num = num==0?this.getName().compareTo(p.getName()):num;
//次要条件3 价格,编号,名称相同,出产地不一定相同
num = num==0?this.getAddress().compareTo(p.getAddress()):num;
return num;*/
/*int n = (int)(this.getPrice()-p.getPrice());
if(n==0){
n = this.getId().compareTo(p.getId());
}
if(n==0){
n = this.getName().compareTo(p.getName());
}
if(n==0){
n = this.getAddress().compareTo(p.getAddress());
}
return n;*/
}
}
- 自定义对象排序
*
* 按照学生的总分排序 使用自然排序
* 1:实现Comparable接口,重写CompareTo方法
*
* 按照学生的总分排序 使用比较器排序
* 2:实现Comparator,重写compare方法
import java.util.Comparator;
import java.util.TreeSet;
public class Demo {
public static void main(String[] args) {
/*TreeSet<Student> ts = new TreeSet<>();
ts.add(new Student("1","张三",20,100,99,88));
ts.add(new Student("2","李四",18,99,88,100));
ts.add(new Student("3","王五",18,78,68,75));
ts.add(new Student("4","赵柳",17,45,68,100));
ts.add(new Student("1","张三",20,100,99,88));*/
TreeSet<Student> ts = new TreeSet<>(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
//主要条件 按照总分排序
int num = s1.result()-s2.result();
//次要条件1 总分相同,学号不一定相同
num = num==0?s1.getId().compareTo(s2.getId()):num;
//次要条件2 总分,学号相同,姓名不一定相同
num = num==0?s1.getName().compareTo(s2.getName()):num;
//次要条件3 总分,学号,姓名相同,年龄不一定相同
num = num==0?(s1.getAge()-s2.getAge()):num;
//次要条件4 总分,学号,姓名,年龄相同,语文不一定相同
num = num==0?(s1.getChinese()-s2.getChinese()):num;
//次要条件5 总分,学号,姓名,年龄,语文相同,数学不一定相同
num = num==0?(s1.getMath()-s2.getMath()):num;
//次要条件6 总分,学号,姓名,年龄,语文,数学相同,英语不一定相同
num = num==0?(s1.getEnglish()-s2.getEnglish()):num;
return num;
}
});
ts.add(new Student("1","张三",20,100,99,88));
ts.add(new Student("2","李四",18,99,88,100));
ts.add(new Student("3","王五",18,78,68,75));
ts.add(new Student("4","赵柳",17,45,68,100));
ts.add(new Student("1","张三",20,100,99,88));
System.out.println("学号\t姓名\t年龄\t语文成绩\t数学成绩\t英语成绩\t总分");
for (Student stu : ts) {
System.out.println(stu.getId()+"\t"+stu.getName()+"\t"+stu.getAge()+"\t"+
stu.getChinese()+"\t"+stu.getMath()+"\t"+stu.getEnglish()+
"\t"+stu.result());
}
}
}
public class Student/* implements Comparable<Student>*/ {
private String id;
private String name;
private int age;
private int chinese;
private int math;
private int english;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
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 int getChinese() {
return chinese;
}
public void setChinese(int chinese) {
this.chinese = chinese;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getEnglish() {
return english;
}
public void setEnglish(int english) {
this.english = english;
}
public Student(String id, String name, int age, int chinese, int math,
int english)
{
super();
this.id = id;
this.name = name;
this.age = age;
this.chinese = chinese;
this.math = math;
this.english = english;
}
public Student() {
super();
}
//总分
public int result(){
return chinese+math+english;
}
/*@Override
public int compareTo(Student s) {
//主要条件 按照总分排序
int num = s.result()-this.result();
//次要条件1 总分相同,学号不一定相同
num = num==0?this.getId().compareTo(s.getId()):num;
//次要条件2 总分,学号相同,姓名不一定相同
num = num==0?this.getName().compareTo(s.getName()):num;
//次要条件3 总分,学号,姓名相同,年龄不一定相同
num = num==0?(this.getAge()-s.getAge()):num;
//次要条件4 总分,学号,姓名,年龄相同,语文不一定相同
num = num==0?(this.getChinese()-s.getChinese()):num;
//次要条件5 总分,学号,姓名,年龄,语文相同,数学不一定相同
num = num==0?(this.getMath()-s.getMath()):num;
//次要条件6 总分,学号,姓名,年龄,语文,数学相同,英语不一定相同
num = num==0?(this.getEnglish()-s.getEnglish()):num;
return num;
}
*/
}