ASP实现类似Java中的Linked HashMap类
来源:asp之家 时间:2010-04-03 20:49:00
'--------------------------------------------------------------
'ASP版HashMap实现
'该实现存储数据为有序数据,类似Java中的LinkedHashMap
'--------------------------------------------------------------
Class HashMap
dim arr()
dim arr_len
'构造函数
private Sub Class_Initialize
'其中 arr(0,n)为key,arr(1,n)为value
arr_len = 0
redim arr(1,arr_len)
End Sub
'========================
'*put方法 往MAP中插入键值对,如果Key存在则覆盖
'@k 键值key
'@v key对应的value值
'========================
public Sub put(k,v)
dim is_update
is_update = false
arr_len = ubound(arr,2)
for i=0 to arr_len-1
if k=arr(0,i) then
arr(1,i) = v
is_update = true
exit for
end if
next
if not is_update then
arr_len = arr_len +1
redim preserve arr(1,arr_len)
arr(0,arr_len-1) = k
arr(1,arr_len-1) = v
end if
End Sub
'========================
'*getv方法 通过key值获取对应的value
'@k 键值key
'========================
public Function getv(k)
dim v
for i=0 to arr_len
if k=arr(0,i) then
v = arr(1,i)
exit for
end if
next
getv = v
End Function
'========================
'*keys方法 获取Map中所有的KEY,以数组形式返回
'========================
public Function keys()
dim v()
Redim v(arr_len)
for i=0 to arr_len-1
v(i) = arr(0,i)
next
keys = v
End Function
'========================
'*values方法 获取Map中所有的Value,以数组形式返回
'========================
public Function values()
dim v()
Redim v(arr_len)
for i=0 to arr_len-1
v(i) = arr(1,i)
next
values = v
End Function
'========================
'*remove方法 从MAP中移除指定key的键值对
'@k 键值key
'========================
public Sub remove(k)
arr_len = ubound(arr,2)
for i=0 to arr_len
if k=arr(0,i) then
v = arr(1,i)
for k = i to arr_len-1
arr(0,k) = arr(0,k+1)
arr(1,k) = arr(1,k+1)
next
arr_len = arr_len - 1
redim preserve arr(1,arr_len)
exit for
end if
next
End Sub
'========================
'*putAll 将源HashMap的所有值添加到当前对象中,如果存在重复key值将覆盖原有值
'@map 源HashMap
'========================
public Function putAll(map)
if typeName(map)<>"HashMap" then
response.Write "putAll只能存放HashMap"
else
for i=0 to map.length-1
put map.keys()(i),map.values()(i)
next
End If
End Function
'========================
'*length属性 Map中的键值对数
'========================
public property get length()
length = arr_len
End property
'========================
'*removeAll方法 移除MAP中所有值
'========================
public Sub removeAll()
arr_len = 0
redim arr(1,1)
End Sub
End Class