类型 | 字节数 |
---|---|
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