来了黑马之后,每天都像打了鸡血一样,这个时间不在敲代码的不正常- package cn.yuxi.study03;
- import java.util.Comparator;
- import java.util.Set;
- import java.util.TreeMap;
- public class TreeMapTest2 {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- // 创建TreeMap集合对象
- TreeMap<Student, String> tm = new TreeMap<Student, String>(
- new Comparator<Student>() {
- @Override
- public int compare(Student s1, Student s2) {
- int num=s1.getAge()-s2.getAge();
- if(num==0)
- {
- return s1.getName().compareTo(s2.getName());
- }
- else return num;
- }
- });
- // 创建学生对象
- Student s1 = new Student("林青霞", 29);
- Student s2 = new Student("成龙", 32);
- Student s3 = new Student("林心如", 23);
- Student s4 = new Student("范冰冰", 34);
- //添加到TreeMap集合中
-
- tm.put(s1, "hm001");
- tm.put(s2, "hm002");
- tm.put(s3, "hm003");
- tm.put(s4, "hm004");
- Set<Student> set=tm.keySet();
- for(Student key:set)
- {
- String value=tm.get(key);
- System.out.println(value+"**"+key.getName()+"**"+key.getAge());
- }
- }
- }
复制代码 |
|