How to implement hashmap in php: Use [construct()] constructor to implement, the code is [Class HashMap{var $H_table;public function __construct(){$this->H_table = a}].
How to implement hashmap in php:
The main method refers to JAVA’s HASHMAP implementation
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); } }
If you want to know more about programming learning, please pay attention to the php training column!
The above is the detailed content of How to implement hashmap in php. For more information, please follow other related articles on the PHP Chinese website!