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类中要复写方法:

扫描二维码关注公众号,回复: 4627320 查看本文章

  
  
  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. }
运行结果:


猜你喜欢

转载自blog.csdn.net/houguofei123/article/details/81712692