• 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;
        }
    }
  • 相关阅读:
    Oracle 游标
    对"com1"的访问被拒绝
    几种不伤身体的速效减肥秘方 生活至上,美容至尚!
    护肤必备,教你如何护理肌肤 生活至上,美容至尚!
    九种食物摆脱便秘烦恼 生活至上,美容至尚!
    1个多月就能看到效果的减肥大法 生活至上,美容至尚!
    防晒涂抹四大要领,让你远离日晒痛苦 生活至上,美容至尚!
    晚间保养四部曲 轻松护肤有妙招 生活至上,美容至尚!
    夏日驱蚊虫蟑螂的最好办法! 生活至上,美容至尚!
    睡前一分钟打造完美下半身 生活至上,美容至尚!
  • 原文地址:https://www.cnblogs.com/cxmsky/p/2864753.html
Copyright © 2020-2023  润新知