首页 >后端开发 >PHP问题 > 正文

php如何实现hashmap

原创2020-09-28 15:24:0002001

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的详细内容,更多请关注php中文网其它相关文章!

php中文网最新课程二维码

声明:本文原创发布php中文网,转载请注明出处,感谢您的尊重!如有疑问,请联系admin@php.cn处理

  • 相关标签:php hashmap
  • 相关文章

    相关视频


    网友评论

    文明上网理性发言,请遵守 新闻评论服务协议

    我要评论
  • 专题推荐

    作者信息

    coldplay.xixi

    好好学习 天天向上

    最近文章
    重点详解Java类和对象1402
    浅析php简单操作mysql锁机制3014
    php无法加载mysql怎么办1590
    推荐视频教程
  • phpStudy V8 视频教程phpStudy V8 视频教程
  • PHP开发免费公益直播课PHP开发免费公益直播课
  • PHP快速操控Excel之PhpSpreadsheetPHP快速操控Excel之PhpSpreadsheet
  • PHP代码整洁之道PHP代码整洁之道
  • 视频教程分类