123456789101112131415161718192021222324252627282930313233343536373839 |
- package J20250723;
- import java.util.Comparator;
- import java.util.Set;
- import java.util.TreeMap;
- /**
- * @author WanJl
- * @version 1.0
- * @title Demo07_TreeMap
- * @description
- * @create 2025/7/23
- */
- public class Demo07_TreeMap {
- public static void main(String[] args) {
- TreeMap<Student,String> map=new TreeMap((Comparator<Student>) (o1, o2) -> {
- int r=o2.getId()-o1.getId();
- r=r==0?o1.getName().compareTo(o2.getName()):r;
- return r;
- });
- Student s1=new Student(1001,"张三1");
- Student s2=new Student(1002,"张三2");
- Student s3=new Student(1003,"张三3");
- Student s4=new Student(1004,"张三4");
- map.put(s1,"北京");
- map.put(s2,"上海");
- map.put(s3,"广州");
- map.put(s4,"深圳");
- //获取key的集合
- Set<Student> students = map.keySet();
- for (Student s:students){
- //get获取值
- String addr = map.get(s);
- System.out.println(s+"->"+addr);
- }
- }
- }
|