-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHashMap.ts
90 lines (80 loc) · 1.27 KB
/
HashMap.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
class HashMap {
//定义长度
//创建一个对象
private obj = {};
/**
* 判断Map是否为空
*/
public isEmpty():boolean {
return Object.keys(this.obj).length == 0;
}
/**
* 判断对象中是否包含给定Key
*/
public containsKey(key):boolean {
return (key in this.obj);
}
/**
* 判断对象中是否包含给定的Value
*/
public containsValue(value):boolean {
for (var key in this.obj) {
if (this.obj[key] == value) {
return true;
}
}
return false;
}
/**
*向map中添加数据
*/
public put(key, value):void {
this.obj[key] = value;
}
/**
* 根据给定的Key获得Value
*/
public get(key):any {
return this.containsKey(key) ? this.obj[key] : null;
}
/**
* 根据给定的Key删除一个值
*/
public remove(key):void {
if (this.containsKey(key)) {
delete this.obj[key]
}
}
/**
* 获得Map中的所有Value
*/
public values():any {
var _values = new Array();
for (var key in this.obj) {
_values.push(this.obj[key]);
}
return _values;
}
/**
* 获得Map中的所有Key
*/
public keySet():any {
var _keys = new Array();
for (var key in this.obj) {
_keys.push(key);
}
return _keys;
}
/**
* 获得Map的长度
*/
public size():number {
return Object.keys(this.obj).length;
}
/**
* 清空Map
*/
public clear():void {
this.obj = {};
}
}