類型 | 字節數 |
---|---|
float | 4 |
double | 8 |
byte | 1 |
short | 2 |
int | 4 |
long | 8 |
char | 2 |
boolean |
public boolean equals(Object obj) { return (this == obj); }
public boolean equals(Object anObject) { if (this == anObject) { return true; } if (anObject instanceof String) { String anotherString = (String)anObject; int n = value.length; if (n == anotherString.value.length) { char v1[] = value; char v2[] = anotherString.value; int i = 0; while (n-- != 0) { if (v1[i] != v2[i]) return false; i++; } return true; } } return false; }
public V put(K key, V value) { if (key == null) return putForNullKey(value); int hash = hash(key.hashCode()); int i = indexFor(hash, table.length); for (Entry<K,V> e = table[i]; e != null; e = e.next) { Object k; if (e.hash == hash && ((k = e.key) == key || key.equals(k))) { V oldValue = e.value; e.value = value; e.recordAccess(this); return oldValue; } } modCount++; addEntry(hash, key, value, i); return null; }
public static void main(String[] args) throws Exception { HashMap hashMap=new HashMap<Person,Integer>(); Person p1=new Person("jiajun",18); Person p2=new Person("jiajun",18); System.out.println("這兩個對象在設置的時候應該是相同的"); hashMap.put(p1,666); System.out.println("那麼按照咱們的設計思路,咱們經過p2應該能夠獲得666"); System.out.println(hashMap.get(p2)); System.out.println("但是這時候輸出的倒是null"); } class Person { String name; int age; public Person(String name,int age) { this.name=name; this.age=age; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if(this.getClass()!=obj.getClass()) { return false; } People p = (People)obj; return this.name.equals(p.name) && this.age == p.age; } }
做者:jiajun 出處: http://www.cnblogs.com/-new/
本文版權歸做者和博客園共有,歡迎轉載,但未經做者贊成必須保留此段聲明,且在文章頁面明顯位置給出原文鏈接,不然保留追究法律責任的權利。若是以爲還有幫助的話,能夠點一下右下角的【推薦】,但願可以持續的爲你們帶來好的技術文章!想跟我一塊兒進步麼?那就【關注】我吧。html