# 手写HashMap

public class MyHashMap<K, V> {
int cap = 16;
int size = 0;
double doubleFactor = 0.75;

@SuppressWarnings({"unchecked"})
Node<K, V>[] array = new Node[cap];

void put(K k, V v) {
checkSize();
int index = k.hashCode() % cap;
if (array[index] == null) {
array[index] = new Node<>(k, v);
return;
}

Node<K, V> point = array[index];
while (point != null) {
if (point.k.equals(k)) {
point.v = v;
return;
}

if (point.next == null) {
point.next = new Node<>(k, v);
size++;
return;
}

point = point.next;
}

}

/**
* 是否含有
*
* @param k
* @return
*/
boolean contains(K k) {
int index = k.hashCode() % cap;
if (array[index] == null) return false;
Node<K, V> point = array[index];
while (point != null) {
if (point.k.equals(k)) return true;
point = point.next;
}
return false;
}

/**
* 获取值
*
* @param k
* @return
*/
V get(K k) {
int index = k.hashCode() % cap;
if (array[index] == null) return null;
Node<K, V> point = array[index];
while (point != null) {
if (point.k.equals(k)) return point.v;
point = point.next;
}
return null;
}

/**
* 带默认值的获取
*
* @param k
* @param v
* @return
*/
V getOrDefault(K k, V v) {
V vGet = get(k);
return vGet == null ? v : vGet;
}

/**
* 移除
* @param k
* @return
*/
boolean remove(K k) {
int index = k.hashCode() % cap;
if (array[index] == null) return false;
array[index] = null;
size--;
return true;
}
size--;
return true;
}
}
return false;
}

/**
* 检查容量
*/
private void checkSize() {
if (cap * doubleFactor <= size) {
doubleArray();
}
}

/**
* 数组加倍
*/
private void doubleArray() {
cap <<= 1;
@SuppressWarnings({"unchecked"})
Node<K, V>[] newArr = new Node[cap];
for (Node<K, V> n : array) {
if (n != null)
newArr[n.k.hashCode() % cap] = n;
}
array = newArr;
}

static class Node<K, V> {
K k;
V v;
Node<K, V> next;

public Node(K k, V v) {
this.k = k;
this.v = v;
}
}

public int size() {
return size;
}

public void printMap() {
for (int i = 0; i < array.length; i++) {
System.out.print(i + " : ");
if (array[i] == null) {
System.out.println("null");
} else {
Node<K, V> p = array[i];
while (p != null) {
System.out.printf("(%s,%s)->", p.k.toString(), p.v.toString());
p = p.next;
}
System.out.print("null");
System.out.println();
}
}
}

public static void main(String[] args) {
MyHashMap<Integer, Character> myHashMap = new MyHashMap<>();
for (int i = 32; i < 120; i++) {
myHashMap.put(i, (char) i);
System.out.print(myHashMap.get(i) + " ");
}
System.out.println();
myHashMap.printMap();

}
}


0 : null
1 : null
2 : null
3 : null
4 : null
5 : null
6 : null
7 : null
8 : null
9 : null
10 : null
11 : null
12 : (76,L)->null
13 : (77,M)->null
14 : (78,N)->null
15 : (79,O)->null
16 : (80,P)->null
17 : (81,Q)->null
18 : (82,R)->null
19 : (83,S)->null
20 : (84,T)->null
21 : (85,U)->null
22 : (86,V)->null
23 : (87,W)->null
24 : (88,X)->null
25 : (89,Y)->null
26 : (90,Z)->null
27 : (91,[)->null
28 : (92,)->null
29 : (93,])->null
30 : (94,^)->null
31 : (95,_)->null
32 : (32, )->(48,0)->(64,@)->(96,`)->null
33 : (33,!)->(49,1)->(65,A)->(97,a)->null
34 : (34,")->(50,2)->(66,B)->(98,b)->null
35 : (35,#)->(51,3)->(67,C)->(99,c)->null
36 : (36,\$)->(52,4)->(68,D)->(100,d)->null
37 : (37,%)->(53,5)->(69,E)->(101,e)->null
38 : (38,&)->(54,6)->(70,F)->(102,f)->null
39 : (39,’)->(55,7)->(71,G)->(103,g)->null
40 : (40,()->(56,8)->(72,H)->(104,h)->null
41 : (41,))->(57,9)->(73,I)->(105,i)->null
42 : (42,*)->(58,:)->(74,J)->(106,j)->null
43 : (43,+)->(59,; )->(75,K)->(107,k)->null
44 : (44,)->(108,l)->null
45 : (45,-)->(109,m)->null
46 : (46,.)->(110,n)->null
47 : (47,/)->(111,o)->null
48 : (112,p)->null
49 : (113,q)->null
50 : (114,r)->null
51 : (115,s)->null
52 : (116,t)->null
53 : (117,u)->null
54 : (118,v)->null
55 : (119,w)->null
56 : null
57 : null
58 : null
59 : null
60 : (60,<)->null
61 : (61,=)->null
62 : (62,>)->null
63 : (63,?)->null

• 点赞
• 评论 1
• 分享
x

海报分享

扫一扫，分享海报

• 收藏
• 手机看

分享到微信朋友圈

x

扫一扫，手机阅读

• 打赏

打赏

FlowingRiver

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文

08-02 627
05-20 392
09-09 157
01-06 1990
07-13 1158
07-22 1645
01-12 234
03-13 4736
06-28 1388