TreeMap类
1.底层数据结构是自平衡的二叉树
2.如何保证元素唯一? 根据二叉树的存的特点
第一个元素进来作为根节点存储
后面元素和根节点一次比较
大了 放在根的左边
小了 放在根的右边
相等 就不存储
3.如何保证元素无序的
因为可排序,所以相对于存储的时候是无序的
4.如何保证元素可排序的
根据二叉树 取 的特点 (中序遍历)
面试题: 已知前序遍历和中序遍历,求后序遍历的结果?
5.TreeMap的数据结构仅针对键有效,和值无关
6.如果键是系统类一般都实现了Comparable接口,如果是自定义对象,需要自己实现Comparable接口【自然排序】或者Comparator接口【比较器排序】
public class TreeMapDemo01 {
public static void main(String[] args) {
TreeMap<Integer, String> ts = new TreeMap<>();
ts.put(18, "abc");
ts.put(20, "efg");
ts.put(15, "hij");
ts.put(25, "opq");
ts.put(14, "hehe");
ts.put(14, "haha");
ts.put(25, "ok");
ts.put(100, "op");
ts.put(67, "zz");
for(Integer i : ts.keySet()) {
System.out.println(i + "=" + ts.get(i));
}
}
}
/*
* 键是员工类,值是员工姓名的情况
*/
public class TreeMapDemo02 {
public static void main(String[] args) {
// TreeMap<Employee, String> tm = new TreeMap<>();//TreeMap 员工类实现Comparator接口
TreeMap<Employee, String> tm = new TreeMap<>(new Comparator<Employee>() {
@Override
public int compare(Employee e1, Employee e2) {
// 如何排序?
// 先按照 编号排序, 编号相等按照工资排序, 工资相等按照姓名排序
// 三目运算符的标准排序格式:
Collator c = Collator.getInstance(Locale.CHINA);//实现中文
int cmp = e1.getId().compareTo(e2.getId());
int cmp2 = (cmp == 0) ? e1.getSalary().compareTo(e2.getSalary()) : cmp;
int cmp3 = (cmp2 == 0) ? c.compare(e1.getName(), e2.getName()) : cmp2;
return cmp3;
}
});
tm.put(new Employee("1001", "张三", 2000.0), "张三");
tm.put(new Employee("1002", "哈哈", 2000.0), "李四");
tm.put(new Employee("1002", "李四", 2600.0), "李四");
tm.put(new Employee("1002", "呵呵", 2000.0), "李四");
tm.put(new Employee("1002", "李四", 2100.0), "李四");
tm.put(new Employee("1002", "李四", 2700.0), "李四");
tm.put(new Employee("1002", "嘿嘿", 2000.0), "李四");
tm.put(new Employee("1002", "李四", 2000.0), "李四");
tm.put(new Employee("1002", "李四", 2000.0), "李四");
tm.put(new Employee("1002", "娃娃", 2000.0), "李四");
tm.put(new Employee("1002", "李四", 2000.0), "李四");
tm.put(new Employee("1002", "李四", 2000.0), "李四");
tm.put(new Employee("1002", "李四", 2000.0), "李四");
tm.put(new Employee("1007", "刘九", 2000.0), "刘九");
tm.put(new Employee("1008", "王十", 2000.0), "王十");
tm.put(new Employee("1009", "肖十一", 2000.0), "肖十一");
tm.put(new Employee("1009", "肖十一", 2000.0), "肖十一");
tm.put(new Employee("1003", "王五", 2000.0), "王五");
tm.put(new Employee("1003", "王五", 2000.0), "王五");
tm.put(new Employee("1006", "钱八", 2000.0), "钱八");
tm.put(new Employee("1004", "赵六", 2000.0), "赵六");
tm.put(new Employee("1005", "孙七", 2000.0), "孙七");
tm.put(new Employee("1006", "钱八", 2000.0), "钱八");
for(Employee e : tm.keySet()) {
String name = tm.get(e);
System.out.println(e + "==" + name);
}
}
}
public class Employee /*implements Comparable<Employee>*/{//实现Comparable接口
private String id;
private String name;
private Double salary;
public Employee() {
super();
}
public Employee(String id, String name, Double salary) {
super();
this.id = id;
this.name = name;
this.salary = salary;
}
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 getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", salary=" + salary + "]";
}
/*@Override
public int compareTo(Employee e) {
// 如何排序?
// 先按照 编号排序, 编号相等按照工资排序, 工资相等按照姓名排序
// 三目运算符的标准排序格式:
Collator c = Collator.getInstance(Locale.CHINA);
int cmp = this.getId().compareTo(e.getId());
int cmp2 = (cmp == 0) ? this.getSalary().compareTo(e.getSalary()) : cmp;
int cmp3 = (cmp2 == 0) ? c.compare(this.getName(), e.getName()) : cmp2;
return cmp3;
}
*/
}