<
script language
=
"
javascript
"
>
2
/*
*
3
*作者 :Fantasy
4
*Email: fantasycs@163.com
5
*QQ : 8635335
6
*Blog : http://www.blogjava.net/fantasy
7
*版本 :V1.1
8
*/
9
function
HashMap()
10
{
11
/*
* Map 大小 *
*/
12
var
size
=
0
;
13
/*
* 对象 *
*/
14
var
entry
=
new
Object();
15
16
/*
* 存 *
*/
17
this
.put
=
function
(key , value)
18
{
19
if
(
!
this
.containsKey(key))
20
{
21
size
++
;
22
}
23
entry[key]
=
value;
24
}
25
26
/*
* 取 *
*/
27
this
.get
=
function
(key)
28
{
29
return
this
.containsKey(key)
?
entry[key] :
null
;
30
}
31
32
/*
* 删除 *
*/
33
this
.remove
=
function
( key )
34
{
35
if
(
this
.containsKey(key)
&&
(
delete
entry[key] ) )
36
{
37
size
--
;
38
}
39
}
40
41
/*
* 是否包含 Key *
*/
42
this
.containsKey
=
function
( key )
43
{
44
return
(key
in
entry);
45
}
46
47
/*
* 是否包含 Value *
*/
48
this
.containsValue
=
function
( value )
49
{
50
for
(
var
prop
in
entry)
51
{
52
if
(entry[prop]
==
value)
53
{
54
return
true
;
55
}
56
}
57
return
false
;
58
}
59
60
/*
* 所有 Value *
*/
61
this
.values
=
function
()
62
{
63
var
values
=
new
Array();
64
for
(
var
prop
in
entry)
65
{
66
values.push(entry[prop]);
67
}
68
return
values;
69
}
70
71
/*
* 所有 Key *
*/
72
this
.keys
=
function
()
73
{
74
var
keys
=
new
Array();
75
for
(
var
prop
in
entry)
76
{
77
keys.push(prop);
78
}
79
return
keys;
80
}
81
82
/*
* Map Size *
*/
83
this
.size
=
function
()
84
{
85
return
size;
86
}
87
88
/*
清空
*/
89
this
.clear
=
function
()
90
{
91
size
=
0
;
92
entry
=
new
Object();
93
}
94
}
95
96
var
map
=
new
HashMap();
97
98
/*
99
map.put("A","1");
100
map.put("B","2");
101
map.put("A","5");
102
map.put("C","3");
103
map.put("A","4");
104
*/
105
106
/*
107
alert(map.containsKey("XX"));
108
alert(map.size());
109
alert(map.get("A"));
110
alert(map.get("XX"));
111
map.remove("A");
112
alert(map.size());
113
alert(map.get("A"));
114
*/
115
116
/*
* 同时也可以把对象作为 Key *
*/
117
/*
118
var arrayKey = new Array("1","2","3","4");
119
var arrayValue = new Array("A","B","C","D");
120
map.put(arrayKey,arrayValue);
121
var value = map.get(arrayKey);
122
for(var i = 0 ; i < value.length ; i++)
123
{
124
//alert(value[i]);
125
}
126
*/
127
/*
* 把对象做为Key时 ,自动调用了该对象的 toString() 方法 其实最终还是以String对象为Key*
*/
128
129
/*
* 如果是自定义对象 那自己得重写 toString() 方法 否则
. 就是下面的结果 *
*/
130
131
function
MyObject(name)
132
{
133
this
.name
=
name;
134
}
135
136
/*
*
137
function MyObject(name)
138
{
139
this.name = name;
140
141
this.toString = function ()
142
{
143
return this.name;
144
}
145
}
146
*
*/
147
var
object1
=
new
MyObject(
"
小张
"
);
148
var
object2
=
new
MyObject(
"
小名
"
);
149
150
map.put(object1,
"
小张
"
);
151
map.put(object2,
"
小名
"
);
152
alert(map.get(object1));
153
alert(map.get(object2));
154
map.remove(
"
xxxxx
"
);
155
alert(map.size());
156
157
/*
* 运行结果 小名 小名 size = 1 *
*/
158
159
/*
* 如果改成复写toString()方法的对象 , 效果就完全不一样了![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
*
*/
160
161
</
script
> http://www.blogjava.net/fantasy/archive/2006/05/06/44742.html
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)