File size: 871 Bytes
0284885
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package TreeMap_package1;

import java.util.TreeMap;

public class TreeMap_demo2 {
    public static void main(String[] args) {
        /*
            TreeMap集合:基本应用
            需求2:
                键:学生对象
                值:籍贯
                要求:按照学生年龄的升序排列,年龄一样按照姓名的字母排序,同姓名年龄视为同一个人。
         */

        //1、创建集合
        TreeMap<Student, String> tm = new TreeMap<>();

        //2、创建3个学生对象
        Student s1 = new Student("zhangsan", 23);
        Student s2 = new Student("lisi", 24);
        Student s3 = new Student("wangwu", 25);

        //3、添加元素
        tm.put(s1, "江苏");
        tm.put(s2, "天津");
        tm.put(s3, "北京");

        //4、打印集合
        System.out.println(tm);

    }
}