[英]Is there Java HashMap equivalent in PHP?
我需要类似于 Java 中的 HashMap 的 PHP 对象,但我在谷歌搜索时没有找到,所以如果有人知道我如何在 PHP 中模仿 HashMap,我们将不胜感激。
PHP 中的数组可以具有键值结构。
Depending on what you want you might be interested in the SPL Object Storage class.根据您的需要,您可能对 SPL 对象存储类感兴趣。
http://php.net/manual/en/class.splobjectstorage.php http://php.net/manual/en/class.splobjectstorage.php
It lets you use objects as keys, has an interface to count, get the hash and other goodies.它让你可以使用对象作为键,有一个接口来计数、获取哈希和其他好东西。
$s = new SplObjectStorage;
$o1 = new stdClass;
$o2 = new stdClass;
$o2->foo = 'bar';
$s[$o1] = 'baz';
$s[$o2] = 'bingo';
echo $s[$o1]; // 'baz'
echo $s[$o2]; // 'bingo'
Open a phpsh terminal:打开一个 phpsh 终端:
php> $myhashmap = array();
php> $myhashmap['mykey1'] = 'myvalue1';
php> $myhashmap['mykey2'] = 'myvalue2';
php> echo $myhashmap['mykey2'];
myvalue2
The complexity of the $myhashmap['mykey2']
in this case appears to be constant time O(1), meaning that as the size of $myhasmap approaches infinity, the amount of time it takes to retrieve a value given a key stays the same.在这种情况下
$myhashmap['mykey2']
的复杂性似乎是恒定时间 O(1),这意味着随着 $myhasmap 的大小接近无穷大,检索给定键值所需的时间保持不变相同的。
Evidence the php array read is constant time: php数组读取的证据是恒定时间:
Run this through the PHP interpreter:通过 PHP 解释器运行:
php> for($x = 0; $x < 1000000000; $x++){
... $myhashmap[$x] = $x . " derp";
... }
The loop adds 1 billion key/values, it takes about 2 minutes to add them all to the hashmap which may exhaust your memory.该循环添加了 10 亿个键/值,将它们全部添加到哈希图中大约需要 2 分钟,这可能会耗尽您的内存。
Then see how long it takes to do a lookup:然后看看做一次查找需要多长时间:
php> system('date +%N');echo " " . $myhashmap[10333] . " ";system('date +%N');
786946389 10333 derp 789008364
So how fast is the PHP array map lookup?那么 PHP 数组映射查找的速度有多快呢?
The 10333
is the key we looked up. 10333
是我们查找的关键。 1 million nanoseconds == 1 millisecond. 100 万纳秒 == 1 毫秒。 The amount of time it takes to get a value from a key is 2.06 million nanoseconds or about 2 milliseconds.
从键中获取值所需的时间为 206 万纳秒或大约 2 毫秒。 About the same amount of time if the array were empty.
如果数组为空,时间大约相同。 This looks like constant time to me.
这对我来说似乎是恒定的时间。
$fruits = array (
"fruits" => array("a" => "Orange", "b" => "Banana", "c" => "Apple"),
"numbers" => array(1, 2, 3, 4, 5, 6),
"holes" => array("first", 5 => "second", "third")
);
echo $fruits["fruits"]["b"]
outputs 'Banana'输出'香蕉'
taken from http://in2.php.net/manual/en/function.array.php取自http://in2.php.net/manual/en/function.array.php
You can make a simple hashMap yourself.您可以自己制作一个简单的 hashMap。 What a hashMap does is storing items in a array using the hash as index/key.
hashMap 所做的是使用散列作为索引/键将项目存储在数组中。 Hash-functions give collisions once in a while (not often, but they may do), so you have to store multiple items for an entry in the hashMap.
散列函数偶尔会产生冲突(不经常,但它们可能会发生),因此您必须在 hashMap 中为一个条目存储多个项目。 That simple is a hashMap:
一个简单的 hashMap:
class IEqualityComparer {
public function equals($x, $y) {
throw new Exception("Not implemented!");
}
public function getHashCode($obj) {
throw new Exception("Not implemented!");
}
}
class HashMap {
private $map = array();
private $comparer;
public function __construct(IEqualityComparer $keyComparer) {
$this->comparer = $keyComparer;
}
public function has($key) {
$hash = $this->comparer->getHashCode($key);
if (!isset($this->map[$hash])) {
return false;
}
foreach ($this->map[$hash] as $item) {
if ($this->comparer->equals($item['key'], $key)) {
return true;
}
}
return false;
}
public function get($key) {
$hash = $this->comparer->getHashCode($key);
if (!isset($this->map[$hash])) {
return false;
}
foreach ($this->map[$hash] as $item) {
if ($this->comparer->equals($item['key'], $key)) {
return $item['value'];
}
}
return false;
}
public function del($key) {
$hash = $this->comparer->getHashCode($key);
if (!isset($this->map[$hash])) {
return false;
}
foreach ($this->map[$hash] as $index => $item) {
if ($this->comparer->equals($item['key'], $key)) {
unset($this->map[$hash][$index]);
if (count($this->map[$hash]) == 0)
unset($this->map[$hash]);
return true;
}
}
return false;
}
public function put($key, $value) {
$hash = $this->comparer->getHashCode($key);
if (!isset($this->map[$hash])) {
$this->map[$hash] = array();
}
$newItem = array('key' => $key, 'value' => $value);
foreach ($this->map[$hash] as $index => $item) {
if ($this->comparer->equals($item['key'], $key)) {
$this->map[$hash][$index] = $newItem;
return;
}
}
$this->map[$hash][] = $newItem;
}
}
For it to function you also need a hash-function for your key and a comparer for equality (if you only have a few items or for another reason don't need speed you can let the hash-function return 0; all items will be put in same bucket and you will get O(N) complexity)为了让它发挥作用,您还需要一个用于键的散列函数和一个用于相等的比较器(如果您只有几个项目或出于其他原因不需要速度,您可以让散列函数返回 0;所有项目都将是放入同一个桶中,您将获得 O(N) 复杂度)
Here is an example:下面是一个例子:
class IntArrayComparer extends IEqualityComparer {
public function equals($x, $y) {
if (count($x) !== count($y))
return false;
foreach ($x as $key => $value) {
if (!isset($y[$key]) || $y[$key] !== $value)
return false;
}
return true;
}
public function getHashCode($obj) {
$hash = 0;
foreach ($obj as $key => $value)
$hash ^= $key ^ $value;
return $hash;
}
}
$hashmap = new HashMap(new IntArrayComparer());
for ($i = 0; $i < 10; $i++) {
for ($j = 0; $j < 10; $j++) {
$hashmap->put(array($i, $j), $i * 10 + $j);
}
}
echo $hashmap->get(array(3, 7)) . "<br/>";
echo $hashmap->get(array(5, 1)) . "<br/>";
echo ($hashmap->has(array(8, 4))? 'true': 'false') . "<br/>";
echo ($hashmap->has(array(-1, 9))? 'true': 'false') . "<br/>";
echo ($hashmap->has(array(6))? 'true': 'false') . "<br/>";
echo ($hashmap->has(array(1, 2, 3))? 'true': 'false') . "<br/>";
$hashmap->del(array(8, 4));
echo ($hashmap->has(array(8, 4))? 'true': 'false') . "<br/>";
Which gives as output:作为输出给出:
37
51
true
false
false
false
false
You could create a custom HashMap class for that in php.您可以在 php 中为此创建一个自定义 HashMap 类。 example as shown below containing the basic HashMap attributes such as get and set.
如下所示的示例包含基本的 HashMap 属性,例如 get 和 set。
class HashMap{
public $arr;
function init() {
function populate() {
return null;
}
// change to 999 for efficiency
$this->arr = array_map('populate', range(0, 9));
return $this->arr;
}
function get_hash($key) {
$hash = 0;
for ($i=0; $i < strlen($key) ; $i++) {
$hash += ord($key[$i]);
}
// arr index starts from 0
$hash_idx = $hash % (count($this->arr) - 1);
return $hash_idx;
}
function add($key, $value) {
$idx = $this->get_hash($key);
if ($this->arr[$idx] == null) {
$this->arr[$idx] = [$value];
} else{
$found = false;
$content = $this->arr[$idx];
$content_idx = 0;
foreach ($content as $item) {
// checking if they have same number of streams
if ($item == $value) {
$content[$content_idx] = [$value];
$found = true;
break;
}
$content_idx++;
}
if (!$found) {
// $value is already an array
array_push($content, $value);
// updating the array
$this->arr[$idx] = $content;
}
}
return $this->arr;
}
function get($key) {
$idx = $this->get_hash($key);
$content = $this->arr[$idx];
foreach ($content as $item) {
if ($item[1] == $key) {
return $item;
break;
}
}
}
}
Hope this was useful希望这是有用的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.