zoukankan      html  css  js  c++  java
  • Java基础之Map学习代码示例一:

    import java.util.*;
    class HashMapDemo
    {
        public static void main(String[] args)
        {
            Map<Student,String> studentMap = new HashMap<Student,String>();
            
            studentMap.put(new Student("张三",21),"山东");
            studentMap.put(new Student("李四",20),"河南");
            studentMap.put(new Student("王五",23),"江苏");
            studentMap.put(new Student("赵六",22),"北京");
            studentMap.put(new Student("刘七",25),"云南");
            studentMap.put(new Student("许八",21),"广西");
            studentMap.put(new Student("赵六",22),"广东");
            
            printHashMap(studentMap);
            
            Map<Student,String> studentMapSort = new TreeMap<Student,String>(new ForAgeSortCompareator());
            
            studentMapSort.putAll(studentMap);
            
            printHashMap(studentMapSort);
        }
        
        public static void printHR()
        {
            System.out.println("------------------------------------------");
        }
        
        public static void printHashMap(Map<Student,String> hashMap)
        {
            for(Iterator<Map.Entry<Student,String>> it = hashMap.entrySet().iterator();it.hasNext();)
            {
                Map.Entry<Student,String> entry = it.next();
                Student student = entry.getKey();
                String address = entry.getValue();
                
                System.out.println(student.getName() + ":"+student.getAge()+"::::::"+address);
            }
            
            printHR();
        }
    }

    class Student implements Comparable
    {
        private String name;
        private int age;
        
        public Student(String name,int age)
        {
            this.setName(name);
            this.setAge(age);
        }
        
        public void setName(String name)
        {
            this.name = name;
        }
        
        public String getName()
        {
            return this.name;
        }
        
        public void setAge(int age)
        {
            this.age = age;
        }
        
        public int getAge()
        {
            return this.age;
        }
        
        public int hashCode()
        {
            return this.getName().hashCode() + this.getAge() * 21;
        }
        
        public boolean equals(Object obj)
        {
            if(!(obj instanceof Student))
                throw new ClassCastException("不是学生对象!");
            
            Student student = (Student)obj;
            
            return (this.getName().equals(student.getName()) && this.getAge()==student.getAge());
        }
        
        public int compareTo(Object obj)
        {
            if(!(obj instanceof Student))
                throw new ClassCastException("不是学生对象!");
                
            Student student = (Student)obj;
            
            int result = this.getName().compareTo(student.getName());
            
            if(result==0)
                return this.getAge()-student.getAge();        
                
            return result;
        }
    }

    class ForAgeSortCompareator implements Comparator<Student>
    {
        public int compare(Student s1,Student s2)
        {
            int result = s1.getAge()-s2.getAge();
            if(result == 0)
                return s1.getName().compareTo(s2.getName());
            
            return result;
        }
    }
  • 相关阅读:
    js简单验证码的生成和验证
    基本够用的php.ini配置文件(CentOS7)
    转发:CentOS下tar压缩排除某个文件夹或文件及解压
    阿里云服务器CentOS7 vsftp安装、设置及后台端口的设置
    转发:entos7修改文件夹权限和用户名用户组
    转发:查看centos中的用户和用户组
    阿里云服务器CentOS7怎么分区格式化/挂载硬盘
    php调试用的几个小方法
    Jquery实现日期转换为 Unix时间戳及时间戳转换日期
    Jquery计算时间戳之间的差值,可返回年,月,日,小时等
  • 原文地址:https://www.cnblogs.com/cxmsky/p/2864753.html
Copyright © 2011-2022 走看看