Hatta’s Blog

Membuat HashMap JAVA di PHP

Posted by: Hatta on: January 13, 2009

Akhirnya… walaupun ga semua method bisa diimplementasikan tapi beberapa hal yang sering terpakai dapat saya implementasikan. Oke ga perlu berpanjang lebar, mari kita lihat scriptnya :

<?php
class HashMap{

private $pool = array();

function HashMap() {}

function put($key,$val){
$this->pool[$key] = $val;
}

function get($key){
return array_key_exists($key,$this->pool) ? $this->pool[$key] : null;
}

function getKeys(){
return array_keys($this->pool);
}

function getValues(){
return array_values($this->pool);
}

function size() {
return count($this->pool);
}
}
?>

Lalu bagaimana cara mengimplementasikannya… berikut cuplikannya

<?php
$testing = new HashMap();
$testing->put(’23′,’15′);
$testing->put(’12′,’13′);

echo ‘$testing->get(“12″) = ‘ . $testing->get(’12′) .”<BR />”;
echo ‘$testing->size() = ‘.$testing->size().’<BR />’;
echo ‘foreach ($testing->getKeys() as $value) = <BR />’;
foreach ($testing->getKeys() as $value) {
echo $value.’<BR />’;
}
echo ‘foreach ($testing->getValues() as $value) = <BR />’;
foreach ($testing->getValues() as $value) {
echo $value.’<BR />’;
}
?>

dan inilah hasilnya…..

$testing->get(“12″) = 13
$testing->size() = 2
foreach ($testing->getKeys() as $value) =
23
12
foreach ($testing->getValues() as $value) =
15
13

selamat mencoba……

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 

January 2009
M T W T F S S
« Dec   Jul »
 1234
567891011
12131415161718
19202122232425
262728293031  

Blog Stats

  • 12,947 hits
Follow

Get every new post delivered to your Inbox.