【黑马程序员济南】Hashmap实现原理和重写Hashmap
1.HashMap的数据结构
1.HashMap的数据结构
数组的特点是:寻址容易,插入和删除困难;而链表的特点是:寻址困难,插入和删除容易。那么我们能不能综合两者的特性,做出一种寻址容易,插入删除也容易的数据结构?答案是肯定的,这就是我们要提起的哈希表,哈希表有多种不同的实现方法,我接下来解释的是最常用的一种方法——拉链法,我们可以理解为“链表的数组”
我们可以发现哈希表是由数组+链表组成的,一个长度为16的数组中,每个元素存储的是一个链表的头结点。那么这些元素是按照什么样的规则存储到数组中呢。一般情况是通过hash(key)%len获得,也就是元素的key的哈希值对数组长度取模得到。比如上述哈希表中,12%16=12,28%16=12,108%16=12,140%16=12。所以12、28、108以及140都存储在数组下标为12的位置。
HashMap其实也是一个线性的数组实现的,所以可以理解为其存储数据的容器就是一个线性数组。这可能让我们很不解,一个线性的数组怎么实现按键值对来存取数据呢?这里HashMap有做一些处理。
1.首先HashMap里面实现一个静态内部类Entry,其重要的属性有 key , value, next,从属性key,value我们就能很明显的看出来Entry就是HashMap键值对实现的一个基础bean,我们上面说到HashMap的基础就是一个线性数组,这个数组就是Entry[],Map里面的内容都保存在Entry[]里面。2.HashMap的存取实现
[if !supportLineBreakNewLine]
[endif]
既然是线性数组,为什么能随机存取?这里HashMap用了一个小算法,大致是这样实现:
[java] view plain copy
[if !supportLineBreakNewLine]
[endif]
[if !supportLists]1. [endif]//存储时:
[if !supportLists]2. [endif]int hash
= key.hashCode();// 这个hashCode方法这里不详述,只要理解每个key的hash是一个固定的int值
[if !supportLists]3. [endif]int index
= hash % Entry[].length;
[if !supportLists]4. [endif]Entry[index]
= value;
[if !supportLists]5. [endif]
[if !supportLists]6. [endif]//取值时:
[if !supportLists]7. [endif]int hash
= key.hashCode();
[if !supportLists]8. [endif]int index
= hash % Entry[].length;
[if !supportLists]9. [endif]return
Entry[index];
到这里我们轻松的理解了HashMap通过键值对实现存取的基本原理
3.疑问:如果两个key通过hash%Entry[].length得到的index相同,会不会有覆盖的危险?
这里HashMap里面用到链式数据结构的一个概念。上面我们提到过Entry类里面有一个next属性,作用是指向下一个Entry。打个比方, 第一个键值对A进来,通过计算其key的hash得到的index=0,记做:Entry[0] = A。一会后又进来一个键值对B,通过计算其index也等于0,现在怎么办?HashMap会这样做:B.next = A,Entry[0] = B,如果又进来C,index也等于0,那么C.next = B,Entry[0] = C;这样我们发现index=0的地方其实存取了A,B,C三个键值对,他们通过next这个属性链接在一起。所以疑问不用担心。也就是说数组中存储的是最后插入的元素。到这里为止,HashMap的大致实现,我们应该已经清楚了。
当然HashMap里面也包含一些优化方面的实现,这里也说一下。比如:Entry[]的长度一定后,随着map里面数据的越来越长,这样同一个index的链就会很长,会不会影响性能?HashMap里面设置一个因素(也称为因子),随着map的size越来越大,Entry[]会以一定的规则加长长度。3.解决hash冲突的办法
开放定址法(线性探测再散列,二次探测再散列,伪随机探测再散列) 再哈希法 链地址法 建立一个公共溢出区
Java中hashmap的解决办法就是采用的链地址法。4.实现自己的HashMap
Entry.java[java] view plain copy
[if !supportLineBreakNewLine]
[endif]
[if !supportLists]1. [endif]package
edu.sjtu.erplab.hash;
[if !supportLists]2. [endif]
[if !supportLists]3. [endif]public
class Entry{
[if !supportLists]4. [endif]
final K key;
[if !supportLists]5. [endif]
V value;
[if !supportLists]6. [endif]
Entry next;//下一个结点
[if !supportLists]7. [endif]
[if !supportLists]8. [endif]
//构造函数
[if !supportLists]9. [endif]
public Entry(K k, V v, Entry n) {
[if !supportLists]10. [endif]
key = k;
[if !supportLists]11. [endif]
value = v;
[if !supportLists]12. [endif]
next = n;
[if !supportLists]13. [endif]
}
[if !supportLists]14. [endif]
[if !supportLists]15. [endif] public final K getKey()
{
[if !supportLists]16. [endif]
return key;
[if !supportLists]17. [endif]
}
[if !supportLists]18. [endif]
[if !supportLists]19. [endif] public final V
getValue() {
[if !supportLists]20. [endif]
return value;
[if !supportLists]21. [endif]
}
[if !supportLists]22. [endif]
[if !supportLists]23. [endif] public final V
setValue(V newValue) {
[if !supportLists]24. [endif] V oldValue =
value;
[if !supportLists]25. [endif]
value = newValue;
[if !supportLists]26. [endif]
return oldValue;
[if !supportLists]27. [endif] }
[if !supportLists]28. [endif]
[if !supportLists]29. [endif] public final boolean
equals(Object o) {
[if !supportLists]30. [endif]
if (!(o instanceof Entry))
[if !supportLists]31. [endif]
return false;
[if !supportLists]32. [endif]
Entry e = (Entry)o;
[if !supportLists]33. [endif]
Object k1 = getKey();
[if !supportLists]34. [endif]
Object k2 = e.getKey();
[if !supportLists]35. [endif]
if (k1 == k2 || (k1 != null && k1.equals(k2)))
{
[if !supportLists]36. [endif]
Object v1 = getValue();
[if !supportLists]37. [endif]
Object v2 = e.getValue();
[if !supportLists]38. [endif]
if (v1 == v2 || (v1 != null &&
v1.equals(v2)))
[if !supportLists]39. [endif]
return true;
[if !supportLists]40. [endif]
}
[if !supportLists]41. [endif]
return false;
[if !supportLists]42. [endif] }
[if !supportLists]43. [endif]
[if !supportLists]44. [endif] public final int
hashCode() {
[if !supportLists]45. [endif]
return (key==null ? 0 : key.hashCode()) ^
(value==null ? 0 : value.hashCode());
[if !supportLists]46. [endif] }
[if !supportLists]47. [endif]
[if !supportLists]48. [endif] public final String
toString() {
[if !supportLists]49. [endif]
return getKey() + "=" + getValue();
[if !supportLists]50. [endif] }
[if !supportLists]51. [endif]
[if !supportLists]52. [endif]}
MyHashMap.java
[java] view plain copy
[if !supportLineBreakNewLine]
[endif]
[if !supportLists]1. [endif]package edu.sjtu.erplab.hash;
[if !supportLists]2. [endif]
[if !supportLists]3. [endif]//保证key与value不为空
[if !supportLists]4. [endif]public
class MyHashMap {
[if !supportLists]5. [endif]
private Entry[] table;//Entry数组表
[if !supportLists]6. [endif]
static final int DEFAULT_INITIAL_CAPACITY = 16;//默认数组长度
[if !supportLists]7. [endif]
private int size;
[if !supportLists]8. [endif]
[if !supportLists]9. [endif]
// 构造函数
[if !supportLists]10. [endif] public MyHashMap()
{
[if !supportLists]11. [endif]
table = new Entry[DEFAULT_INITIAL_CAPACITY];
[if !supportLists]12. [endif]
size = DEFAULT_INITIAL_CAPACITY;
[if !supportLists]13. [endif]
}
[if !supportLists]14. [endif]
[if !supportLists]15. [endif] //获取数组长度
[if !supportLists]16. [endif] public int getSize()
{
[if !supportLists]17. [endif]
return size;
[if !supportLists]18. [endif] }
[if !supportLists]19. [endif]
[if !supportLists]20. [endif] // 求index
[if !supportLists]21. [endif]
static int indexFor(int h, int length) {
[if !supportLists]22. [endif]
return h % (length - 1);
[if !supportLists]23. [endif] }
[if !supportLists]24. [endif]
[if !supportLists]25. [endif] //获取元素
[if !supportLists]26. [endif] public V get(Object key)
{
[if !supportLists]27. [endif]
if (key == null)
[if !supportLists]28. [endif]
return null;
[if !supportLists]29. [endif]
int hash = key.hashCode();// key的哈希值
[if !supportLists]30. [endif]
int index = indexFor(hash, table.length);// 求key在数组中的下标
[if !supportLists]31. [endif]
for (Entry e = table[index]; e !=
null; e = e.next) {
[if !supportLists]32. [endif]
Object k = e.key;
[if !supportLists]33. [endif]
if (e.key.hashCode() == hash && (k == key ||
key.equals(k)))
[if !supportLists]34. [endif]
return e.value;
[if !supportLists]35. [endif]
}
[if !supportLists]36. [endif]
return null;
[if !supportLists]37. [endif] }
[if !supportLists]38. [endif]
[if !supportLists]39. [endif] // 添加元素
[if !supportLists]40. [endif] public V put(K key, V
value) {
[if !supportLists]41. [endif]
if (key == null)
[if !supportLists]42. [endif]
return null;
[if !supportLists]43. [endif]
int hash = key.hashCode();
[if !supportLists]44. [endif]
int index = indexFor(hash, table.length);
[if !supportLists]45. [endif]
[if !supportLists]46. [endif]
// 如果添加的key已经存在,那么只需要修改value值即可
[if !supportLists]47. [endif]
for (Entry e = table[index]; e != null; e =
e.next) {
[if !supportLists]48. [endif]
Object k = e.key;
[if !supportLists]49. [endif]
if (e.key.hashCode() == hash && (k == key ||
key.equals(k))) {
[if !supportLists]50. [endif]
V oldValue = e.value;
[if !supportLists]51. [endif]
e.value = value;
[if !supportLists]52. [endif]
return oldValue;// 原来的value值
[if !supportLists]53. [endif]
}
[if !supportLists]54. [endif]
}
[if !supportLists]55. [endif]
// 如果key值不存在,那么需要添加
[if !supportLists]56. [endif]
Entry e = table[index];// 获取当前数组中的e
[if !supportLists]57. [endif]
table[index] = new Entry(key, value, e);// 新建一个Entry,并将其指向原先的e
[if !supportLists]58. [endif]
return null;
[if !supportLists]59. [endif] }
[if !supportLists]60. [endif]
[if !supportLists]61. [endif]}
MyHashMapTest.java[java] view plain copy
[if !supportLineBreakNewLine]
[endif]
[if !supportLists]1. [endif]package
edu.sjtu.erplab.hash;
[if !supportLists]2. [endif]
[if !supportLists]3. [endif]public
class MyHashMapTest {
[if !supportLists]4. [endif]
[if !supportLists]5. [endif]
public static void main(String[] args) {
[if !supportLists]6. [endif]
[if !supportLists]7. [endif]
MyHashMap map = new
MyHashMap();
[if !supportLists]8. [endif]
map.put(1, 90);
[if !supportLists]9. [endif]
map.put(2, 95);
[if !supportLists]10. [endif]
map.put(17, 85);
[if !supportLists]11. [endif]
[if !supportLists]12. [endif]
System.out.println(map.get(1));
[if !supportLists]13. [endif]
System.out.println(map.get(2));
[if !supportLists]14. [endif]
System.out.println(map.get(17));
[if !supportLists]15. [endif]
System.out.println(map.get(null));
[if !supportLists]16. [endif] }
[if !supportLists]17. [endif]}
网友评论