PHP实现HashMap
来源:互联网 发布:蓝纹奶酪 知乎 编辑:程序博客网 时间:2024/05/17 02:52
Class HashMap{
var $H_table;
/*
* HashMap构造函数
*/
public function __construct() {
$this->H_table = array ();
}
/*
*向HashMap中添加一个键值对
*@param $key 插入的键
*@param $value 插入的值
*/
public function put($key, $value) {
if (!array_key_exists($key, $this->H_table)) {
$this->H_table[$key] = $value;
return null;
} else {
$tempValue = $this->H_table[$key];
$this->H_table[$key] = $value;
return $tempValue;
}
}
/*
* 根据key获取对应的value
* @param $key
*/
public function get($key) {
if (array_key_exists($key, $this->H_table))
return $this->H_table[$key];
else
return null;
}
/*
*移除HashMap中所有键值对
*/
/*
*删除指定key的键值对
*@param $key 要移除键值对的key
*/
public function remove($key) {
$temp_table = array ();
if (array_key_exists($key, $this->H_table)) {
$tempValue = $this->H_table[$key];
while ($curValue = current($this->H_table)) {
if (!(key($this->H_table) == $key))
$temp_table[key($this->H_table)] = $curValue;
next($this->H_table);
}
$this->H_table = null;
$this->H_table = $temp_table;
return $tempValue;
} else
return null;
}
/**
* 获取HashMap的所有键值
* @return 返回HashMap中key的集合,以数组形式返回
*/
public function keys(){
return array_keys($this->H_table);
}
/**
* 获取HashMap的所有value值
*/
public function values(){
return array_values($this->H_table);
}
/**
* 将一个HashMap的值全部put到当前HashMap中
* @param $map
*/
public function putAll($map){
if(!$map->isEmpty()&& $map->size()>0){
$keys = $map->keys();
foreach($keys as $key){
$this->put($key,$map->get($key));
}
}
}
/**
* 移除HashMap中所有元素
*/
public function removeAll() {
$this->H_table = null;
$this->H_table = array ();
}
/*
*HashMap中是否包含指定的值
*@param $value
*/
public function containsValue($value) {
while ($curValue = current($this->H_table)) {
if ($curValue == $value) {
return true;
}
next($this->H_table);
}
return false;
}
/*
*HashMap中是否包含指定的键key
*@param $key
*/
public function containsKey($key) {
if (array_key_exists($key, $this->H_table)) {
return true;
} else {
return false;
}
}
/*
*获取HashMap中元素个数
*/
public function size() {
return count($this->H_table);
}
/*
*判断HashMap是否为空
*/
public function isEmpty() {
return (count($this->H_table) == 0);
}
/**
*
*/
public function toString() {
print_r($this->H_table);
}
}
- PHP实现HashMap
- HashMap实现
- HashMap实现
- HashMap实现
- HashMap实现
- HashMap(4)--动手实现HashMap
- javascript 实现 HashMap
- ASP实现HashMap
- 双层HashMap实现检索
- Java HashMap实现详解
- HashMap的实现原理
- Java HashMap实现原理
- HashMap的实现原理
- HashMap实现多线程安全
- hashMap的实现原理
- HashMap的实现原理
- hashmap C语言实现
- hashmap实现机制
- Windows中实现定时自动运行程序
- apache搭建svn在windows下配置成功
- oracle 编码问题 讨论解决
- myeclipse 8.5.0 下载
- 项目经理团队建设该干的事(等待天将降大任于私的那天。。。来见证一下)
- PHP实现HashMap
- c# IDE 快捷键
- OpenNMS架构介绍
- IBM Rational Purify
- 桌子的情调,从LED开始……
- application/x-www-form-urlencoded等字符编码的解释说明
- 回归自然
- eclipse集成velocity插件
- Eclipse 自动提示代码功能消失的解决方法