1. 程式人生 > 其它 >集合框架-Map集合-HashMap儲存自定義物件

集合框架-Map集合-HashMap儲存自定義物件

 1 package cn.itcast.p6.hashmap.demo;
 2 
 3 import java.util.HashMap;
 4 import java.util.Iterator;
 5 import java.util.Set;
 6 
 7 import cn.itcast.p2.bean.Student;
 8 
 9 public class HashMapDemo {
10 
11     public static void main(String[] args) {
12         // TODO Auto-generated method stub
13         /*
14 * 將學生物件HashMap<K, V>通過鍵與值儲存到map集合中。 15 */ 16 17 HashMap<Student,String> hm = new HashMap<Student,String>(); 18 hm.put(new Student("lisi",38), "北京"); 19 hm.put(new Student("zhaoliu",24), "上海"); 20 hm.put(new Student("xiaoqiang",31), "瀋陽");
21 hm.put(new Student("wangcai",28), "大連"); 22 hm.put(new Student("zhaoliu",24), "鐵嶺"); 23 24 // Set<Student> keySet = hm.keySet(); 25 // 26 // Iterator<Student> it = keySet.iterator(); 27 28 Iterator<Student> it = hm.keySet().iterator();
29 while (it.hasNext()) { 30 Student key = it.next(); 31 String value = hm.get(key); 32 System.out.println(key.getName()+":"+key.getAge()+"---"+value); 33 } 34 } 35 36 }
HashMapDemo
 1 package cn.itcast.p2.bean;
 2 
 3 public class Person implements Comparable<Person>{
 4     private String name;
 5     private int age;
 6     
 7     
 8     public Person() {
 9         super();
10         // TODO Auto-generated constructor stub
11     }
12     
13     public Person(String name, int age) {
14         super();
15         this.name = name;
16         this.age = age;
17     }
18     public int compareTo(Person p) {//指定比較型別,傳入比較型別
19 //        Person p= (Person)obj;
20         int temp = this.age-p.age;
21         return temp==0?this.name.compareTo(p.name):temp;
22     }
23     
24     
25 @Override
26     public int hashCode() {
27         final int prime = 31;
28         int result = 1;
29         result = prime * result + age;
30         result = prime * result + ((name == null) ? 0 : name.hashCode());
31         return result;
32     }
33 
34     @Override
35     public boolean equals(Object obj) {
36         if (this == obj)
37             return true;
38         if (obj == null)
39             return false;
40         if (getClass() != obj.getClass())
41             return false;
42         Person other = (Person) obj;
43         if (age != other.age)
44             return false;
45         if (name == null) {
46             if (other.name != null)
47                 return false;
48         } else if (!name.equals(other.name))
49             return false;
50         return true;
51     }
52 
53     //    @Override
54 //    public boolean equals(Object obj) {//不能把Object改為Person,equals方法來自Object,Object沒有定義泛型
55 //        // TODO Auto-generated method stub
56 //        if (this == obj) {
57 //            return true;
58 //        }
59 //        if (!(obj instanceof Person)) {
60 //            throw new RuntimeException()
61 //        }
62 //        Person p = (Person)obj;
63 //        return super.equals(obj);
64 //    }
65     /*
66      * @Override public int hashCode() { final int prime = 31; int result = 1;
67      * result = prime * result + age; result = prime * result + ((name == null) ? 0
68      * : name.hashCode()); return result; }
69      * 
70      * @Override public boolean equals(Object obj) { if (this == obj) return true;
71      * if (obj == null) return false; if (getClass() != obj.getClass()) return
72      * false; Person other = (Person) obj; if (age != other.age) return false; if
73      * (name == null) { if (other.name != null) return false; } else if
74      * (!name.equals(other.name)) return false; return true; }//alt+shift+s
75      * hashCode和equals
76      */
77     public String getName() {
78         return name;
79     }
80     public void setName(String name) {
81         this.name = name;
82     }
83     public int getAge() {
84         return age;
85     }
86     public void setAge(int age) {
87         this.age = age;
88     }
89 
90     @Override
91     public String toString() {
92         return "Person:"+getName()+":"+getAge();
93     }
94     
95     
96 }
Person
 1 package cn.itcast.p2.bean;
 2 
 3 public class Student extends Person {
 4 
 5     public Student() {
 6         super();
 7         // TODO Auto-generated constructor stub
 8     }
 9 
10     public Student(String name, int age) {
11         super(name, age);
12         // TODO Auto-generated constructor stub
13     }
14 
15     @Override
16     public String toString() {
17         // TODO Auto-generated method stub
18         return "Student:"+getName()+":"+getAge();
19     }
20     
21 }
Student