package Student2;
import java.util.Set;
import java.util.TreeMap;
public class TreeMapDemo {
public static void main(String[] args) {
//建立map对象
TreeMap<Student,String> map = new TreeMap<Student,String>(new MyComparator());
//建立student元素对象
Student s1 = new Student("郭德纲",45);
Student s2 = new Student("于谦",55);
Student s3 = new Student("冯巩",40);
Student s4 = new Student("牛群",42);
Student s5 = new Student("冯巩",43);
Student s6 = new Student("陈佩斯",45);
Student s7 = new Student("朱时茂",35);
Student s8 = new Student("冯巩",40);
//添加元素
map.put(s1,"编号001");
map.put(s2,"编号002");
map.put(s3,"编号003");
map.put(s4,"编号004");
map.put(s5,"编号005");
map.put(s6,"编号006");
map.put(s7,"编号007");
map.put(s8,"编号008");
//遍历map
Set<Student> set = map.keySet();
for (Student key : set) {
String value = map.get(key);
System.out.println(value +"***"+key.getAge()+"***"+key.getName());
}
}
}
|
|