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……
Recent Comments