• JAVA高级教程Java TreeMap表达式(8)


    目录

    
    public class Students implements Comparable<Students>{
        private String name;
        private  int stuNO;
    
        public Students() {
        }
    
        public Students(String age, int stuNO) {
            this.name = age;
            this.stuNO = stuNO;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String age) {
            this.name = age;
        }
    
        public int getStuNO() {
            return stuNO;
        }
    
        public void setStuNO(int stuNO) {
            this.stuNO = stuNO;
        }
    
        @Override
        public String toString() {
            return "Students{" +
                    "age='" + name + '\'' +
                    ", stuNO=" + stuNO +
                    '}';
        }
    
        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Students students = (Students) o;
            return stuNO == students.stuNO && Objects.equals(name, students.name);
        }
    
        @Override
        public int hashCode() {
            return Objects.hash(name, stuNO);
        }
    
        @Override
        public int compareTo(Students o) {
            int n1=this.name.compareTo(o.getName());
            int n2=this.stuNO-o.getStuNO();
            return n2;
        }
    
    • 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
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    package Map01;
    
    import java.util.HashMap;
    import java.util.Map;
    import java.util.TreeMap;
    
    public class Demo03_TreeMap {
        public static void main(String[] args) {
            TreeMap<Students,String> stu=new TreeMap<>();
    
    
            Students p1=new Students("刘德华",20);
            Students p2=new Students("李小龙",22);
            Students p3=new Students("彭于晏",24);
    
            System.out.println("数据的个数"+stu.size());
            System.out.println(stu.toString());
    
            stu.put(p1,"上海");
            stu.put(p2,"北京");
            stu.put(p3,"深圳");
            //重写了hascode就不会重读
            stu.put(new Students("彭于晏",24),"深圳");
    
    
            System.out.println("数据的个数"+stu.size());
            System.out.println(stu.toString());
    
    
            //删除
            stu.remove(p1);
    
    
            //遍历
            //使用keyset
            System.out.println("============使用keyset======================");
    //        Set keys= map.keySet();
            for (Students key : stu.keySet()) {
                System.out.println(key+":"+stu.get(key));
            }
    
            //使用entryset遍历
            System.out.println("============使用entryset遍历======================");
    //        Set> entries=map.entrySet();
            for (Map.Entry<Students, String> entry : stu.entrySet()) {
                System.out.println(entry.getKey()+":"+entry.getValue());
    
            }
    
        }
    }
    
    • 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
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
  • 相关阅读:
    css实现Chrome标签栏
    AI推介-大语言模型LLMs论文速览(arXiv方向):2024.03.05-2024.03.10—(2)
    2023年云计算的发展趋势如何?
    从 JPA 2.x 迁移到 3.0
    Servlet规范之注解与可插拔性
    python基于PHP+MySQL的教学资源管理平台
    牛视系统源码定制开发,抖音矩阵系统,come here。。
    刷题11.27
    python 二手车数据分析以及价格预测
    Spring Cloud Alibaba Ribbon负载均衡器
  • 原文地址:https://blog.csdn.net/Leoon123/article/details/133969652