PHP 中是否有 Java HashMap 等效项?
我需要类似于Java中的HashMap的PHP对象,但我在谷歌搜索时没有找到,所以如果有人知道我如何在PHP中模仿HashMap,我们将不胜感激。
I need PHP object similar to HashMap in Java, but I didn't find when I googled, so if someone knows how I can mimic HashMaps in PHP, help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
PHP 中的数组可以具有键值结构。
Arrays in PHP can have Key Value structure.
在 PHP 中创建类似 Java 的 HashMap,读取复杂度为 O(1)。
打开 phpsh 终端:
在这种情况下,
$myhashmap['mykey2']
的复杂度似乎是常数时间 O(1),这意味着当 $myhasmap 的大小接近无穷大时,在给定键的情况下检索值所需的时间保持不变。证明 php 数组读取的时间是恒定的:
通过 PHP 解释器运行此操作:
循环添加 10 亿个键/值,大约需要 2 分钟才能将它们全部添加到 hashmap,这可能会耗尽您的内存。
然后看看查找一次需要多长时间:
那么 PHP 数组映射查找的速度有多快?
10333
是我们查找的键。 100 万纳秒 == 1 毫秒。从键获取值所需的时间为 206 万纳秒或大约 2 毫秒。如果数组为空,则时间大约相同。对我来说,这看起来像是恒定的时间。Create a Java like HashMap in PHP with O(1) read complexity.
Open a phpsh terminal:
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.Evidence the php array read is constant time:
Run this through the PHP interpreter:
The loop adds 1 billion key/values, it takes about 2 minutes to add them all to the hashmap which may exhaust your memory.
Then see how long it takes to do a lookup:
So how fast is the PHP array map lookup?
The
10333
is the key we looked up. 1 million nanoseconds == 1 millisecond. The amount of time it takes to get a value from a key is 2.06 million nanoseconds or about 2 milliseconds. About the same amount of time if the array were empty. This looks like constant time to me.根据您的需求,您可能对 SPL 对象存储类感兴趣。
http://php.net/manual/en/class.splobjectstorage.php
它允许您使用对象作为键,有一个接口来计数、获取哈希值和其他好处。
Depending on what you want you might be interested in the SPL Object Storage class.
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.
输出 'Banana'
取自 https://www.php.net/manual/ en/function.array.php
outputs 'Banana'
taken from https://www.php.net/manual/en/function.array.php
HashMap 还可以使用除字符串和整数以外的键,读取复杂度为 O(1)(取决于您自己的哈希函数的质量)。
你可以自己制作一个简单的hashMap。 hashMap 的作用是使用哈希作为索引/键将项目存储在数组中。哈希函数偶尔会发生冲突(不常见,但可能会发生),因此您必须为 hashMap 中的条目存储多个项目。这个简单的就是 hashMap:
要使其发挥作用,您还需要一个用于键的哈希函数和一个用于相等的比较器(如果您只有几个项目或由于其他原因不需要速度,您可以让哈希函数返回0;所有项目都将放入同一个桶中,您将获得 O(N) 复杂度)
这是一个示例:
输出如下:
HashMap that also works with keys other than strings and integers with O(1) read complexity (depending on quality of your own hash-function).
You can make a simple hashMap yourself. What a hashMap does is storing items in a array using the hash as index/key. 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. That simple is a hashMap:
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)
Here is an example:
Which gives as output:
您可以在 php.ini 中为此创建一个自定义 HashMap 类。
如下所示的示例包含基本的 HashMap 属性,例如 get 和 set。
希望这有用
You could create a custom HashMap class for that in php.
example as shown below containing the basic HashMap attributes such as get and set.
Hope this was useful