Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there Java HashMap equivalent in PHP?

Tags:

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.

like image 527
newbie Avatar asked Jul 27 '11 08:07

newbie


People also ask

Is there a HashMap in PHP?

HashMaps is a custom PHP Library that allows support for Java HashMaps in PHP.

What can I use instead of HashMap?

If your matrix is static, consider using 2d array instead of a list of a lists. Otherwise, try to set the capacity of the ArrayList to some estimated value, instead of the default value.

Does PHP have a map?

An array in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more.

Are PHP arrays hash maps?

PHP Arrays - Associative Arrays or Hash Maps. Associative array or hash maps are listings of key and value pairs with a posibility to nest additional keys and values. An associative array is a very powerful construct within PHP.


3 Answers

Arrays in PHP can have Key Value structure.

like image 195
sushil bharwani Avatar answered Oct 10 '22 13:10

sushil bharwani


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.

$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' 
like image 26
Boris Gordon Avatar answered Oct 10 '22 14:10

Boris Gordon


Create a Java like HashMap in PHP with O(1) read complexity.

Open a phpsh terminal:

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.

Evidence the php array read is constant time:

Run this through the PHP interpreter:

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.

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?

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.

like image 41
Eric Leschinski Avatar answered Oct 10 '22 13:10

Eric Leschinski