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;
        }
    }
  • 相关阅读:
    从句分析
    artDialog ( v 6.0.2 ) content 参数引入页面 html 内容
    Java实现 LeetCode 13 罗马数字转整数
    Java实现 LeetCode 13 罗马数字转整数
    Java实现 LeetCode 13 罗马数字转整数
    Java实现 LeetCode 12 整数转罗马数字
    Java实现 LeetCode 12 整数转罗马数字
    Java实现 LeetCode 12 整数转罗马数字
    Java实现 LeetCode 11 盛最多水的容器
    Java实现 LeetCode 11 盛最多水的容器
  • 原文地址:https://www.cnblogs.com/cxmsky/p/2864753.html
Copyright © 2011-2022 走看看