双列集合Map的特点
Posted zjm1999
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了双列集合Map的特点相关的知识,希望对你有一定的参考价值。
1 import java.util.HashMap; 2 import java.util.Map; 3 /** 4 * 双列集合Map的特点: 5 * 如果键是自定义对象,必须重写equals和hashcode方法 6 * @author 罗摩衔那 7 * 8 */ 9 public class Demo_Maps 10 { 11 public static void main(String[] args) 12 { 13 //自定义一个容器 14 Map<IdCard,people>map=new HashMap<>(); 15 //初始两个键 16 IdCard idcard1=new IdCard("张三丰", "001"); 17 IdCard idcard2=new IdCard("张三丰", "001"); 18 //对应键值 19 map.put(idcard1, new people("张三丰", "男", "200")); 20 map.put(idcard2, new people("张三丰", "男", "100")); 21 System.out.println(map); 22 } 23 } 24 25 class people 26 { 27 private String name; 28 private String gender; 29 private String age; 30 31 public people(String name,String gender,String age) 32 { 33 this.name=name; 34 this.gender=gender; 35 this.age=age; 36 } 37 38 @Override 39 public String toString() { 40 return "name=" + name + ", gender=" + gender + ", age=" + age ; 41 } 42 43 @Override 44 public int hashCode() { 45 final int prime = 31; 46 int result = 1; 47 result = prime * result + ((age == null) ? 0 : age.hashCode()); 48 result = prime * result + ((gender == null) ? 0 : gender.hashCode()); 49 result = prime * result + ((name == null) ? 0 : name.hashCode()); 50 return result; 51 } 52 53 @Override 54 public boolean equals(Object obj) { 55 if (this == obj) 56 return true; 57 if (obj == null) 58 return false; 59 if (getClass() != obj.getClass()) 60 return false; 61 people other = (people) obj; 62 if (age == null) { 63 if (other.age != null) 64 return false; 65 } else if (!age.equals(other.age)) 66 return false; 67 if (gender == null) { 68 if (other.gender != null) 69 return false; 70 } else if (!gender.equals(other.gender)) 71 return false; 72 if (name == null) { 73 if (other.name != null) 74 return false; 75 } else if (!name.equals(other.name)) 76 return false; 77 return true; 78 } 79 80 } 81 82 class IdCard 83 { 84 private String name; 85 private String ID; 86 public IdCard(String name,String ID) 87 { 88 this.name=name; 89 this.ID=ID; 90 } 91 @Override 92 public String toString() { 93 return "name=" + name + ", ID=" + ID ; 94 } 95 @Override 96 public int hashCode() { 97 final int prime = 31; 98 int result = 1; 99 result = prime * result + ((ID == null) ? 0 : ID.hashCode()); 100 result = prime * result + ((name == null) ? 0 : name.hashCode()); 101 return result; 102 } 103 @Override 104 public boolean equals(Object obj) { 105 if (this == obj) 106 return true; 107 if (obj == null) 108 return false; 109 if (getClass() != obj.getClass()) 110 return false; 111 IdCard other = (IdCard) obj; 112 if (ID == null) { 113 if (other.ID != null) 114 return false; 115 } else if (!ID.equals(other.ID)) 116 return false; 117 if (name == null) { 118 if (other.name != null) 119 return false; 120 } else if (!name.equals(other.name)) 121 return false; 122 return true; 123 } 124 }
1 import java.util.HashMap; 2 import java.util.Map; 3 /** 4 * 双列集合Map的特点: 5 * 键值对只能是意义对应 6 * 添加键值对的时候出现了键重复,新的值会把对应的之前的值覆盖 7 * 8 * @author 罗摩衔那 9 * 10 */ 11 public class Demo_Maps 12 { 13 public static void main(String[] args) 14 { 15 Map<String,String>map=new HashMap<>(); 16 map.put("001", "刘亦菲"); 17 map.put("002", "迪丽热巴"); 18 System.out.println(map); 19 map.put("001", "赵灵儿"); 20 System.out.println(map); 21 } 22 }
以上是关于双列集合Map的特点的主要内容,如果未能解决你的问题,请参考以下文章