1. 程式人生 > >HashMap儲存自定義物件

HashMap儲存自定義物件

將學生物件和學生的歸屬地通過鍵與值儲存到map集合中。


    
  1. import java.util.HashMap;
  2. import java.util.Iterator;
  3. import
    java.util.Set;
  4. class Student {
  5. String name;
  6. int age;
  7. public Student(String name,int age){
  8. super
    ();
  9. this.name=name;
  10. this.age=age;
  11. }
  12. public String getName(){
  13. return name;
  14. }
  15. public void setName(String name){
  16. this.name=name;
  17. }
  18. public int getAge(){
  19. return age;
  20. }
  21. public void setAge(int age){
  22. this.age=age;
  23. }
  24. }
  25. public class HashMapDemo{
  26. public static void main(String[] args){
  27. HashMap<Student,String> hm= new HashMap<Student,String>();
  28. hm.put( new Student( “lisi”, 38), “北京”);
  29. hm.put( new Student( “zhaosi”, 34), “上海”);
  30. hm.put( new Student( “xiaoqiang”, 31), “瀋陽”);
  31. hm.put( new Student( “wangcai”, 28), “大連”);
  32. hm.put( new Student( “zhaosi”, 34), “鐵嶺”);
  33. Set<Student> keySet=hm.keySet();
  34. Iterator<Student> it=keySet.iterator();
  35. while(it.hasNext()){
  36. Student key=it.next();
  37. String value=hm.get(key);
  38. System.out.println(key.getName()+ “:”+key.getAge()+ “…”+value);
  39. }
  40. }
  41. }
執行結果:


當鍵一樣時,其值並沒有被覆蓋,所以要判斷鍵是否相同,HashMap需要定義hashCode和equals方法,在Student類中要複寫方法:


    
  1. import java.util.HashMap;
  2. import java.util.Iterator;
  3. import java.util.Set;
  4. class Student {
  5. String name;
  6. int age;
  7. public Student(String name,int age){
  8. super();
  9. this.name=name;
  10. this.age=age;
  11. }
  12. public String getName(){
  13. return name;
  14. }
  15. public void setName(String name){
  16. this.name=name;
  17. }
  18. public int getAge(){
  19. return age;
  20. }
  21. public void setAge(int age){
  22. this.age=age;
  23. }
  24. public int hashCode(){ //複寫hashCode()
  25. return name.hashCode()+age;
  26. }
  27. public boolean equals(Object obj){ //複寫equals
  28. if( this==obj) return true;
  29. if(!(obj instanceof Student)) throw new ClassCastException( "型別錯誤"); //輸入型別錯誤
  30. Student p = (Student)obj; //強制轉換
  31. return this.name.equals(p.name) && this.age==p.age; //說明姓名和年齡相同則為同一元素
  32. }
  33. }
  34. public class HashMapDemo{
  35. public static void main(String[] args){
  36. HashMap<Student,String> hm= new HashMap<Student,String>();
  37. hm.put( new Student( "lisi", 38), "北京");
  38. hm.put( new Student( "zhaosi", 34), "上海");
  39. hm.put( new Student( "xiaoqiang", 31), "瀋陽");
  40. hm.put( new Student( "wangcai", 28), "大連");
  41. hm.put( new Student( "zhaosi", 34), "鐵嶺");
  42. Set<Student> keySet=hm.keySet();
  43. Iterator<Student> it=keySet.iterator();
  44. //以下被註釋的程式碼可以替換上面兩句:
  45. //Iterator<Student> it=hm.keySet().iterator();
  46. while(it.hasNext()){
  47. Student key=it.next();
  48. String value=hm.get(key);
  49. System.out.println(key.getName()+ ":"+key.getAge()+ "..."+value);
  50. }
  51. }
  52. }
執行結果:


將學生物件和學生的歸屬地通過鍵與值儲存到map集合中。


  
  1. import java.util.HashMap;
  2. import java.util.Iterator;
  3. import java.util.Set;
  4. class Student {
  5. String name;
  6. int age;
  7. public Student(String name,int age){
  8. super();
  9. this.name=name;
  10. this.age=age;
  11. }
  12. public String getName(){
  13. return name;
  14. }
  15. public void setName(String name){
  16. this.name=name;
  17. }
  18. public int getAge(){
  19. return age;
  20. }
  21. public void setAge(int age){
  22. this.age=age;
  23. }
  24. }
  25. public class HashMapDemo{
  26. public static void main(String[] args){
  27. HashMap<Student,String> hm= new HashMap<Student,String>();
  28. hm.put( new Student( “lisi”, 38), “北京”);
  29. hm.put( new Student( “zhaosi”, 34), “上海”);
  30. hm.put( new Student( “xiaoqiang”, 31), “瀋陽”);
  31. hm.put( new Student( “wangcai”, 28), “大連”);
  32. hm.put( new Student( “zhaosi”, 34), “鐵嶺”);
  33. Set<Student> keySet=hm.keySet();
  34. Iterator<Student> it=keySet.iterator();
  35. while(it.hasNext()){
  36. Student key=it.next();
  37. String value=hm.get(key);
  38. System.out.println(key.getName()+ “:”+key.getAge()+ “…”+value);
  39. }
  40. }
  41. }
執行結果:


當鍵一樣時,其值並沒有被覆蓋,所以要判斷鍵是否相同,HashMap需要定義hashCode和equals方法,在Student類中要複寫方法:


  
  1. import java.util.HashMap;
  2. import java.util.Iterator;
  3. import java.util.Set;
  4. class Student {
  5. String name;
  6. int age;
  7. public Student(String name,int age){
  8. super();
  9. this.name=name;
  10. this.age=age;
  11. }
  12. public String getName(){
  13. return name;
  14. }
  15. public void setName(String name){
  16. this.name=name;
  17. }
  18. public int getAge(){
  19. return age;
  20. }
  21. public void setAge(int age){
  22. this.age=age;
  23. }
  24. public int hashCode(){ //複寫hashCode()
  25. return name.hashCode()+age;
  26. }
  27. public boolean equals(Object obj){ //複寫equals
  28. if( this==obj) return true;
  29. if(!(obj instanceof Student)) throw new ClassCastException( "型別錯誤"); //輸入型別錯誤
  30. Student p = (Student)obj; //強制轉換
  31. return this.name.equals(p.name) && this.age==p.age; //說明姓名和年齡相同則為同一元素
  32. }
  33. }
  34. public class HashMapDemo{
  35. public static void main(String[] args){
  36. HashMap<Student,String> hm= new HashMap<Student,String>();
  37. hm.put( new Student( "lisi", 38), "北京");
  38. hm.put( new Student( "zhaosi", 34), "上海");
  39. hm.put( new Student( "xiaoqiang", 31), "瀋陽");
  40. hm.put( new Student( "wangcai", 28), "大連");
  41. hm.put( new Student( "zhaosi", 34), "鐵嶺");
  42. Set<Student> keySet=hm.keySet();
  43. Iterator<Student> it=keySet.iterator();
  44. //以下被註釋的程式碼可以替換上面兩句:
  45. //Iterator<Student> it=hm.keySet().iterator();