HashMap的应用

作者在 2010-05-21 10:39:02 发布以下内容
import java.util.Set;
import java.util.HashMap;
import java.util.Collection;
import java.util.Iterator;

public class TestHashMap{
    public static void main(String[] args) {
        HashMap hm = new HashMap();
        hm.put(new Integer(1003),new Person(1003,"张三",15));
        hm.put(new Integer(1008),new Person(1008,"李四",25));
        hm.put(1015,new Person(1015,"王五",73));   //自动封装
        hm.put(1001,new Person(1001,"赵六",49));

        System.out.println("----检索单个元素----");
        Person p = (Person)hm.get(1008);
        System.out.println(p);
        
        System.out.println("----遍历所有\"键\"(元素名)----");
        Set names = hm.keySet();
        for(Object o : names){
public class Person implements java.lang.Comparable{
    private final int id;
    private String name;
    private int age;
    
    public Person(int id,String name,int age){
        this.id = id;
        this.name = name;
        this.age = age;    
    }
    public int getId(){
        return id;    
    }
    public void setName(String name){
        this.name = name;
    }
    public String getName(){
        return name;    
    }
    public void setAge(int age){
        this.age = age;    
    }
    public int getAge(){
        return age;    
    }
    public String toString(){
        return "Id: " + id + "\tName: " + name + "\tAge: " + age;    
    }    
    @Override
    public int compareTo(Object o){
        Person p = (Person)o;
        return this.id - p.id;        
    }
    @Override
    public boolean equals(Object o){
        boolean flag = false;
        if(o instanceof Person){
            if(this.id == ((Person)o).id)
                flag = true;
        }
        return false;        
    }    
}

            System.out.println(o);
        }
        
        System.out.println("----遍历所有\"值\"(元素值)----");
        Collection values = hm.values();
        for(Object o : values){
            System.out.println(o);
        }
    }
}
集合 | 阅读 1082 次
文章评论,共0条
游客请输入验证码
浏览275759次