`
zzw0309452
  • 浏览: 10180 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
最近访客 更多访客>>
社区版块
存档分类
最新评论

HashMap学习

 
阅读更多
HashMap存放的时候如何判断key是否重复?
如果key是同一个对象,有相同的hashcode和且equals方法相等,那么认为key重复。


看代码了解hashmap
public class Client {
public static void main(String []args) {
HashMap map = new HashMap(10);
A a1 = new A(1,"ss");
A a2 = new A(1,"ss");
map.put(a1, "aaa");
map.put(a2, "bbb");
System.out.println(map.size());
}
}
class A {
int i;String s;
public A(int i, String s) {
this.i = i;
this.s = s;
}
}
返回结果:2

加了hashCode方法
public class Client {
public static void main(String []args) {
HashMap map = new HashMap(10);
A a1 = new A(1,"ss");
A a2 = new A(1,"ss");
map.put(a1, "aaa");
map.put(a2, "bbb");
System.out.println(map.size());
}
}
class A {
int i;
String s;
public A(int i, String s) {

this.i = i;
this.s = s;
}
public int hashCode() {
return i;
}
}
返回结果: 2

加上equals方法

public class Client {
public static void main(String []args) {
HashMap map = new HashMap(10);
A a1 = new A(1,"ss");
A a2 = new A(1,"ss");
map.put(a1, "aaa");
map.put(a2, "bbb");
System.out.println(map.size());
}
}
class A {
int i;String s;
public A(int i, String s) {
this.i = i;
this.s = s;
}
public int hashCode() {
return i;
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final A other = (A) obj;
if (i != other.i)
return false;
if (s == null) {
if (other.s != null)
return false;
} else if (!s.equals(other.s))
return false;
return true;
}
}
返回结果: 1

说明当我们将一个类作为HashMap的key时,必须同时覆盖hashCode和equals方法。否则hashmap的处理结果可能不是我们预料的。至于为什么要这么做呢,我们看看hashmap的put方法就明白了
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;
    }
hashmap内部定义了一个Entry的数组,Entry是一个键值对,并有next成员变量指向它的下一个Entry
static class Entry<K,V> implements Map.Entry<K,V> {
        final K key;
        V value;
        Entry<K,V> next;

当我们想hashmap中put一组值的时候,hashmap根据key(也就是我们实现了hashcode和equals方法的类)的hashcode方法返回的值计算出数组的索引,然后根据索引取出表中的Entry,并遍历,判断Entry中的key是否equals当前要插入的key,如果返回true则覆盖以前的value。


如果2个不同类的对象,有相同的hashcode,并且equals返回true,那么hashmap也会认为他们相同。例子如下
public class Client {

public static void main(String []args) {

HashMap map = new HashMap(10);
A a1 = new A(1,"ss");
B a2 = new B(1,"ss");
map.put(a1, "aaa");
map.put(a2, "bbb");
System.out.println(map.size());

}
}

class A {
int i;
String s;
public A(int i, String s) {

this.i = i;
this.s = s;
}
public int hashCode() {
return i;
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final A other = (A) obj;
if (i != other.i)
return false;
if (s == null) {
if (other.s != null)
return false;
} else if (!s.equals(other.s))
return false;
return true;
}
}

class B {
int i;
String s;
public B(int i, String s) {

this.i = i;
this.s = s;
}
public int hashCode() {
return i;
}
public boolean equals(Object obj) {

A a = (A)obj;
if(this.i == a.i)
return true;
return false;
}
}
返回结果: 1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics