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中文网其他相关文章!
声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章
<🎜>:种植花园 - 完整的突变指南
3 周前ByDDD
<🎜>:泡泡胶模拟器无穷大 - 如何获取和使用皇家钥匙
3 周前By尊渡假赌尊渡假赌尊渡假赌
如何修复KB5055612无法在Windows 10中安装?
3 周前ByDDD
北端:融合系统,解释
3 周前By尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆树的耳语 - 如何解锁抓钩
3 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器