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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值