package com.mengge.test;
import java.util.Map;
import java.util.TreeMap;;
public class TestTreeMap {
public static void main(String[] args) {
// Map<Integer, String> myMap = new TreeMap<Integer, String>();
// Map<Integer, String> map = new java.util.TreeMap<>();
//
// map.put(1, "列宁");
// map.put(3, "斯达克");
// map.put(100, "时代大厦");
// map.put(22, "阿斯达");
// map.put(88, "uuuiw");
//
// for (Integer keyIndex : map.keySet()) {
// System.out.println("key:" + keyIndex + ", value:" + map.get(keyIndex));
// }
MyEmployee mye1 = new MyEmployee(10, "张三", 1000);
MyEmployee mye2 = new MyEmployee(90, "李四", 90000);
MyEmployee mye3 = new MyEmployee(20, "王五", 3000);
MyEmployee mye4 = new MyEmployee(30, "赵六", 3000);
MyEmployee mye5 = new MyEmployee(900, "王五", 9000);
MyEmployee mye6 = new MyEmployee(80, "王七", 7000);
MyEmployee mye7 = new MyEmployee(22, "王八", 90000);
MyEmployee mye8 = new MyEmployee(9, "王九", 1000);
Map<MyEmployee, Integer> objectMap = new TreeMap<MyEmployee, Integer>();
objectMap.put(mye1, mye1.getNid());
objectMap.put(mye2, mye2.getNid());
objectMap.put(mye3, mye3.getNid());
objectMap.put(mye4, mye4.getNid());
objectMap.put(mye5, mye5.getNid());
objectMap.put(mye6, mye6.getNid());
objectMap.put(mye7, mye7.getNid());
objectMap.put(mye8, mye8.getNid());
for (MyEmployee emp : objectMap.keySet()) {
System.out.println("姓名:" + emp.getName() + ", 薪资:" + emp.getSalary() + ", id:" + emp.getNid());
}
}
}
class MyEmployee implements Comparable<MyEmployee> {
private int nid;
private String name;
private double salary;
public MyEmployee(int nid, String name, double salary) {
super();
this.nid = nid;
this.name = name;
this.salary = salary;
}
public int getNid() {
return nid;
}
public void setNid(int nid) {
this.nid = nid;
}
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 int compareTo(MyEmployee o) {
if (this.salary < o.salary) {
return -1;
}else if (this.salary > o.salary) {
return 1;
}else {
if (this.nid < o.nid) {
return -1;
}else if (this.nid > o.nid) {
return 1;
}else {
return 0;
}
}
}
}