HashMap.php 3.2 KB

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