Hash Table
Implementasi Hash Table
Hash table adalah arrays dengan sel-sel yang ukurannya telah
ditentukan dan dapat berisi data atau key yang berkesesuaian
dengan data
Fungsi hash harus memiliki sifat berikut:
- Mudah dihitung.
- Membagi key secara rata pada seluruh sel.
- Sebuah fungsi hash sederhana adalah menggunakan fungsi mod (sisa bagi) dengan bilangan prima.
- Dapat menggunakan manipulasi digit dengan kompleksitas rendah dan distribusi key yang rata
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* @author Nabila Zakiyah | |
*/ | |
import java.io.*; | |
import java.util.*; | |
public class Main { | |
private Hashtable phoneBooks; | |
public Main(){ | |
this.phoneBooks = new Hashtable(); | |
} | |
public void tambah(String name, String number){ | |
if(this.phoneBooks.containsKey(name)){ | |
System.out.println(name+ " exists."); | |
} else { | |
this.phoneBooks.put(name, number); | |
System.out.println(name+" sukses ditambahkan."); | |
} | |
} | |
public void cari (String name){ | |
if(this.phoneBooks.containsKey(name)){ | |
String number = (String) this.phoneBooks.get(name); | |
System.out.println(name+"'s number is "+number+"."); | |
} else { | |
System.out.println("Tidak ada orang bernama '"+name+"'"); | |
} | |
} | |
public void status(){ | |
Enumeration names; | |
names = this.phoneBooks.keys(); | |
int total = 0; | |
while(names.hasMoreElements()) { | |
String str = (String) names.nextElement(); | |
System.out.println(str + " " + this.phoneBooks.get(str)); | |
total++; | |
} | |
System.out.println("Total "+total+" kontak."); | |
} | |
public void hapus_entry(String name){ | |
if(this.phoneBooks.containsKey(name)){ | |
this.phoneBooks.remove(name); | |
System.out.println(name+" telah dihapus."); | |
} else { | |
System.out.println("Tidak ada orang bernama '"+name+"'"); | |
} | |
} | |
public static void main(String[] args ) throws IOException { | |
Scanner scanner = new Scanner(System.in); | |
Main phoneBook = new Main(); | |
while(true){ | |
System.out.print("Masukkan kata pertama dari command\n"); | |
System.out.print("1. tambah kontak \n"); | |
System.out.print("2. cari kontak \n"); | |
System.out.print("3. Status kontak \n"); | |
System.out.print("4. hapus kontak \n"); | |
System.out.print("5. exit \n"); | |
System.out.print("Command: "); | |
int command = scanner.nextInt(); | |
if(command==1){ | |
System.out.print("Nama kontak: "); | |
String name = scanner.next(); | |
System.out.print("Nomor Handphone: "); | |
String number = scanner.next(); | |
phoneBook.tambah(name, number); | |
} | |
else if(command==2){ | |
System.out.print("Nama kontak: "); | |
String name = scanner.next(); | |
phoneBook.cari(name); | |
} | |
else if(command==3){ | |
phoneBook.status(); | |
} | |
else if(command==4){ | |
System.out.print("Nama kontak: "); | |
String name = scanner.next(); | |
phoneBook.hapus_entry(name); | |
} | |
else if(command==5){ | |
break; | |
} | |
} | |
scanner.close(); | |
} | |
} |
Komentar
Posting Komentar