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;
    }
}
原文地址:https://www.cnblogs.com/cxmsky/p/2864753.html