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培训栏目!

网页名称:php如何实现hashmap
转载注明:https://www.cdcxhl.com/article48/cggsep.html

成都网站建设公司_创新互联,为您提供企业建站标签优化网站收录建站公司网站营销关键词优化

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

营销型网站建设