當我們想要創建一個集合,該集合里面的元素都具有唯一性時。會遇到兩種情況:
A:元素為String類型,可以直接用Hashset<String>集合來創建
String類重寫了hashCode()和equals()方法,所以,它就可以把內容相同的字符串去掉。只留下一個。
B:當元素為自定義對象的時候,那么,就要在這個對象的類中重寫hashCode()和equals()方法
下圖是思路:
其實,扯了這么多,上面那些只要理解思路就行了。。。這玩意不用自己寫
在類上面 Alt+Shift+s ,再點h,就能生成相對應的重寫的hashCode()和equls()方法了
代碼體現:
首先創建一個對象類:
1 package zl_HashCode; 2 3 public class Animal { 4 5 private String name; 6 private String color; 7 private int age; 8 public Animal() { 9 super(); 10 // TODO Auto-generated constructor stub 11 } 12 public Animal(String name, String color, int age) { 13 super(); 14 this.name = name; 15 this.color = color; 16 this.age = age; 17 } 18 public String getName() { 19 return name; 20 } 21 public void setName(String name) { 22 this.name = name; 23 } 24 public String getColor() { 25 return color; 26 } 27 public void setColor(String color) { 28 this.color = color; 29 } 30 public int getAge() { 31 return age; 32 } 33 public void setAge(int age) { 34 this.age = age; 35 } 36 @Override 37 public int hashCode() { 38 final int prime = 31; 39 int result = 1; 40 result = prime * result + age; 41 result = prime * result + ((color == null) ? 0 : color.hashCode()); 42 result = prime * result + ((name == null) ? 0 : name.hashCode()); 43 return result; 44 } 45 @Override 46 public boolean equals(Object obj) { 47 if (this == obj) 48 return true; 49 if (obj == null) 50 return false; 51 if (getClass() != obj.getClass()) 52 return false; 53 Animal other = (Animal) obj; 54 if (age != other.age) 55 return false; 56 if (color == null) { 57 if (other.color != null) 58 return false; 59 } else if (!color.equals(other.color)) 60 return false; 61 if (name == null) { 62 if (other.name != null) 63 return false; 64 } else if (!name.equals(other.name)) 65 return false; 66 return true; 67 } 68 69 70 }
具體測試類:
1 package zl_HashCode; 2 /* 3 HashSet集合存儲自定義對象並遍歷。如果對象的成員變量值相同即為同一個對象 4 5 注意了: 6 你使用的是HashSet集合,這個集合的底層是哈希表結構。 7 而哈希表結構底層依賴:hashCode()和equals()方法。 8 如果你認為對象的成員變量值相同即為同一個對象的話,你就應該重寫這兩個方法。 9 如何重寫呢?不同擔心,自動生成即可。 10 */ 11 import java.util.HashSet; 12 13 public class HashCodeTest1 { 14 15 public static void main(String[] args) { 16 17 HashSet<String> hs = new HashSet<String>(); 18 //首先導入String類型的元素,看效果 19 hs.add("阿貓"); 20 hs.add("阿狗"); 21 hs.add("花花"); 22 hs.add("阿貓"); 23 hs.add("阿狗"); 24 hs.add("草草"); 25 //遍歷集合1 26 for(String s1 : hs){ 27 //HashSet集合中存儲String類型,元素會唯一性 28 System.out.println(s1);//花花草草阿狗阿貓 29 } 30 31 HashSet<Animal> ha = new HashSet<Animal>(); 32 33 //再創建自定義對象的元素並導入 34 Animal a1 = new Animal("荷蘭豬","粉白",2); 35 Animal a2 = new Animal("泰迪犬","棕色",1); 36 Animal a3 = new Animal("哈士奇","白色",3); 37 Animal a4 = new Animal("荷蘭豬","粉白",2); 38 Animal a5 = new Animal("泰迪犬","棕色",2); 39 Animal a6 = new Animal("荷蘭豬","黑白",2); 40 41 ha.add(a1); 42 ha.add(a2); 43 ha.add(a3); 44 ha.add(a4); 45 ha.add(a5); 46 ha.add(a6); 47 48 for(Animal a : ha){ 49 System.out.println(a.getName()+"\t"+a.getColor()+"\t"+a.getAge()); 50 /* 對象類中重寫了hashCode()和equals()方法,只讓存儲進唯一元素 51 若是不重寫,則由於每個自定義對象的hashCode值不一樣,所以都能存儲進去 52 泰迪犬 棕色 1 53 泰迪犬 棕色 2 54 荷蘭豬 粉白 2 55 哈士奇 白色 3 56 荷蘭豬 黑白 2 57 */ 58 } 59 } 60 61 }