数据元素可以排序且不可重复
对比:Set接口:HashSet,元素必须重写hashcode 和 equals方法。
去重:比较等于 0 即重复。
确保key可以排序或者提供比较器
元素可以排序 java.lang.Comparable + compareTo new TreeSet().
public class Worker implements java.lang.Comparable {
//工种
private String type;
//工资
private double salary;
public Worker() {
// TODO Auto-generated constructor stub
}
public Worker(String type, double salary) {
super();
this.type = type;
this.salary = salary;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
/**
* 按工资升序
*/
@Override
public int compareTo(Worker o) {
return this.salary>o.salary?1:( this.salary==o.salary?0:-1);
}
@Override
public String toString() {
return "工种:"+this.type+",工资:"+this.salary+"\n";
}
}
import java.util.TreeSet;
/**
* 实体类实现Comparable 接口的应用
* @author Administrator
*
*/
public class TreeSetDemo2 {
/**
* @param args
*/
public static void main(String[] args) {
Worker w1 =new Worker("垃圾回收员",12000);
Worker w2 =new Worker("农民工",8000);
Worker w3 =new Worker("程序猿",5000);
TreeSet employees =new TreeSet();
employees.add(w1);
employees.add(w2);
employees.add(w3);
System.out.println(employees);
}
}
public class Worker implements java.lang.Comparable {
//工种
private String type;
//工资
private double salary;
public Worker() {
// TODO Auto-generated constructor stub
}
public Worker(String type, double salary) {
super();
this.type = type;
this.salary = salary;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
/**
* 按工资升序
*/
@Override
public int compareTo(Worker o) {
return this.salary>o.salary?1:( this.salary==o.salary?0:-1);
}
@Override
public String toString() {
return "工种:"+this.type+",工资:"+this.salary+"\n";
}
}
import java.util.TreeMap;
public class TreeMapDemo02 {
/**
* @param args
*/
public static void main(String[] args) {
Worker w1 =new Worker("垃圾回收员",12000);
Worker w2 =new Worker("农民工",8000);
Worker w3 =new Worker("程序猿",5000);
TreeMap employees =new TreeMap();
employees.put(w1,"bjsxt");
employees.put(w2,"bjsxt");
employees.put(w3,"bjsxt");
System.out.println(employees.keySet());
}
}
2)排序业务类 java.uti.Comparator + compare new TreeSet(Comparator super E > comparator)
注意:在添加数据时排序,数据更改不会·影响原来的顺序,不要修改数据,否则可能重复
//TreeSet
public class Person {
private final String name;//名称
private final int handsome;//帅气指数
public Person() {
name =null;
handsome =0;
}
public Person(String name, int handsome) {
super();
this.name = name;
this.handsome = handsome;
}
public String getName() {
return name;
}
public int getHandsome() {
return handsome;
}
@Override
public String toString() {
return "姓名:"+this.name+",帅气指数:"+this.handsome+"\n";
}
}
/**
* 提供了 解耦的方式:业务排序类
* @author Administrator
*
*/
public class TreeSetDemo {
/**
* @param args
*/
public static void main(String[] args) {
Person p1 =new Person("您",100);
Person p2 =new Person("刘德华",1000);
Person p3 =new Person("梁朝伟",1200);
Person p4 =new Person("老裴",50);
//依次存放到TreeSet容器中,使用排序的业务类(匿名内部类)
TreeSet persons =new TreeSet(
new java.util.Comparator(){
@Override
public int compare(Person o1, Person o2) {
return -(o1.getHandsome()-o2.getHandsome());
}
}
);
persons.add(p1);
//TreeSet 在添加数据时排序
persons.add(p2);
persons.add(p3);
persons.add(p4);
System.out.println(persons);
/*
//改变数据
p4.setHandsome(100);
p4.setName("您");
*/
//p4 与p1 内容重复
System.out.println(persons);
}
}
//TreeMap
public class Worker implements java.lang.Comparable {
//工种
private String type;
//工资
private double salary;
public Worker() {
// TODO Auto-generated constructor stub
}
public Worker(String type, double salary) {
super();
this.type = type;
this.salary = salary;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
/**
* 按工资升序
*/
@Override
public int compareTo(Worker o) {
return this.salary>o.salary?1:( this.salary==o.salary?0:-1);
}
@Override
public String toString() {
return "工种:"+this.type+",工资:"+this.salary+"\n";
}
}
import java.util.Set;
import java.util.TreeMap;
public class TreeMapDemo {
/**
* @param args
*/
public static void main(String[] args) {
Person p1 =new Person("您",100);
Person p2 =new Person("刘德华",1000);
Person p3 =new Person("梁朝伟",1200);
Person p4 =new Person("老裴",50);
TreeMap map =new TreeMap(
new java.util.Comparator(){
@Override
public int compare(Person o1, Person o2) {
return -(o1.getHandsome()-o2.getHandsome());
}
}
);
map.put(p1, "bjsxt");
map.put(p2, "bjsxt");
map.put(p3, "bjsxt");
map.put(p4, "bjsxt");
//查看键
Set persons =map.keySet();
System.out.println(persons);
}
}
TreeSet添加自定义元素:
TreeSet要注意的事项:
1. 往TreeSet添加元素的时候,如果元素本身具备了自然顺序的特性,那么就按照元素自然顺序的特性进行排序存储。
2. 往TreeSet添加元素的时候,如果元素本身不具备自然顺序的特性,那么该元素所属的类必须要实现Comparable接口,把元素的比较规则定义在compareTo(T o)方法上。
3. 如果比较元素的时候,compareTo方法返回 的是0,那么该元素就被视为重复元素,不允许添加.(注意:TreeSet与HashCode、equals方法是没有任何关系。)
4. 往TreeSet添加元素的时候, 如果元素本身没有具备自然顺序 的特性,而元素所属的类也没有实现Comparable接口,那么必须要在创建TreeSet的时候传入一个比较器。
5. 往TreeSet添加元素的时候,如果元素本身不具备自然顺序的特性,而元素所属的类已经实现了Comparable接口, 在创建TreeSet对象的时候也传入了比较器那么是以比较器的比较规则优先使用。