php怎么实现hashmap

发布时间:2020-09-29 14:49:32 作者:小新
来源:亿速云 阅读:287

小编给大家分享一下php怎么实现hashmap,希望大家阅读完这篇文章后大所收获,下面让我们一起去探讨吧!

php实现hashmap的方法:使用【construct()】构造函数实现,代码为【Class HashMap{var $H_table;public function __construct(){$this->H_table = a}】。

php实现hashmap的方法:

主要方法参照JAVA的HASHMAP实现的

Class HashMap{
 var $H_table;
 
  public function __construct() {
   $this->H_table = array ();
  }
 
 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;
  }
  }
 
 
  public function get($key) {
   if (array_key_exists($key, $this->H_table))
    return $this->H_table[$key];
   else
    return null;
  }
 
 
  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;
  }
 
 
  public function keys(){
   return array_keys($this->H_table);
  }
 
  public function values(){
   return array_values($this->H_table);
  }
 
 
  public function putAll($map){
  if(!$map->isEmpty()&& $map->size()>0){
    $keys = $map->keys();
   foreach($keys as $key){
    $this->put($key,$map->get($key));
    }
   }
  }
 
 
  public function removeAll() {
   $this->H_table = null;
   $this->H_table = array ();
  }
 
  public function containsValue($value) {
    while ($curValue = current($this->H_table)) {
    if ($curValue == $value) {
     return true;
    }
    next($this->H_table);
    }
    return false;
  }
 
  public function containsKey($key) {
    if (array_key_exists($key, $this->H_table)) {
    return true;
    } else {
    return false;
    }
  }
 
  public function size() {
   return count($this->H_table);
  }
 
 
 
  public function isEmpty() {
   return (count($this->H_table) == 0);
  }
 
  public function toString() {
  print_r($this->H_table);
  }
}

看完了这篇文章,相信你对php怎么实现hashmap有了一定的了解,想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

推荐阅读:
  1. HashMap的内部实现
  2. HashMap你了解多少

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php hashmap

上一篇:网站换新域如何解析

下一篇:网站能绑定多个域名吗

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》