HashMapHelper.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: chenkuan
  5. * Date: 2017/11/14
  6. * Time: 上午11:13
  7. */
  8. namespace common\helpers;
  9. class HashMapHelper
  10. {
  11. var $H_table;
  12. /*
  13. * HashMap构造函数
  14. */
  15. public function __construct() {
  16. $this->H_table = array ();
  17. }
  18. /*
  19. *向HashMap中添加一个键值对
  20. *@param $key 插入的键
  21. *@param $value 插入的值
  22. */
  23. public function put($key, $value) {
  24. if (!array_key_exists($key, $this->H_table)) {
  25. $this->H_table[$key] = $value;
  26. return null;
  27. } else {
  28. $tempValue = $this->H_table[$key];
  29. $this->H_table[$key] = $value;
  30. return $tempValue;
  31. }
  32. }
  33. /*
  34. * 根据key获取对应的value
  35. * @param $key
  36. */
  37. public function get($key) {
  38. if (array_key_exists($key, $this->H_table))
  39. return $this->H_table[$key];
  40. else
  41. return null;
  42. }
  43. /*
  44. *移除HashMap中所有键值对
  45. */
  46. /*
  47. *删除指定key的键值对
  48. *@param $key 要移除键值对的key
  49. */
  50. public function remove($key) {
  51. $temp_table = array ();
  52. if (array_key_exists($key, $this->H_table)) {
  53. $tempValue = $this->H_table[$key];
  54. while ($curValue = current($this->H_table)) {
  55. if (!(key($this->H_table) == $key))
  56. $temp_table[key($this->H_table)] = $curValue;
  57. next($this->H_table);
  58. }
  59. $this->H_table = null;
  60. $this->H_table = $temp_table;
  61. return $tempValue;
  62. } else
  63. return null;
  64. }
  65. /**
  66. * 获取HashMap的所有键值
  67. * @return 返回HashMap中key的集合,以数组形式返回
  68. */
  69. public function keys(){
  70. return array_keys($this->H_table);
  71. }
  72. /**
  73. * 获取HashMap的所有value值
  74. */
  75. public function values(){
  76. return array_values($this->H_table);
  77. }
  78. /**
  79. * 将一个HashMap的值全部put到当前HashMap中
  80. * @param $map
  81. */
  82. public function putAll($map){
  83. if(!$map->isEmpty()&& $map->size()>0){
  84. $keys = $map->keys();
  85. foreach($keys as $key){
  86. $this->put($key,$map->get($key));
  87. }
  88. }
  89. }
  90. /**
  91. * 移除HashMap中所有元素
  92. */
  93. public function removeAll() {
  94. $this->H_table = null;
  95. $this->H_table = array ();
  96. }
  97. /*
  98. *HashMap中是否包含指定的值
  99. *@param $value
  100. */
  101. public function containsValue($value) {
  102. while ($curValue = current($this->H_table)) {
  103. if ($curValue == $value) {
  104. return true;
  105. }
  106. next($this->H_table);
  107. }
  108. return false;
  109. }
  110. /*
  111. *HashMap中是否包含指定的键key
  112. *@param $key
  113. */
  114. public function containsKey($key) {
  115. if (array_key_exists($key, $this->H_table)) {
  116. return true;
  117. } else {
  118. return false;
  119. }
  120. }
  121. /*
  122. *获取HashMap中元素个数
  123. */
  124. public function size() {
  125. return count($this->H_table);
  126. }
  127. /*
  128. *判断HashMap是否为空
  129. */
  130. public function isEmpty() {
  131. return (count($this->H_table) == 0);
  132. }
  133. /**
  134. *
  135. */
  136. public function toString() {
  137. print_r($this->H_table);
  138. }
  139. }