Implementasi Hash Table
Apa itu Hash Table
Hash table merupakan sebutan pada struktur data yang menggunakan teknik hashing untuk menyimpan sebuah struktur data. Teknik hashing sendiri merupakan teknik yang mengubah data menjadi key, selanjutnya key akan digunakan sebagai indeks tempat disimpannya data. Untuk mendapatkan key sebuah data, akan dibutuhkan hash function.
Hash Function
Hash function dipakai untuk mendapatkan key dari sebuah data. Beberapa bentuk hash function, yaitu :
- Truncation,
- Folding, dan
- Modular Aritmethics.
Implementasi Hash Table
Berikut ini merupakan salah satu contoh penggunaan hash table untuk menyimpan kontak
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
/** | |
* Main Class for contact book application | |
* | |
* @author Anggito Anju | |
* @version ver 1.0 - 20 June 21 | |
*/ | |
import java.util.Hashtable; | |
import java.util.Scanner; | |
// Main class | |
public class ContactBookApp | |
{ | |
// Hash method to make the hash code | |
public static int hashFunction(String uname) { | |
int hashCode = 0; | |
// Using folding type | |
int i = 0; | |
while(i+1<uname.length()) { | |
hashCode += uname.charAt(i) + uname.charAt(i+1); | |
i++; | |
} | |
return hashCode; | |
} | |
// Main method | |
public static void main(String[] args) { | |
Hashtable<Integer, String> contacts = new Hashtable<>(); | |
Scanner inpObj = new Scanner(System.in); | |
while(true) { | |
// User Taunt | |
System.out.println(" ===== BUKU KONTAK ===== "); | |
System.out.println("1. Tambahkan Kontak"); | |
System.out.println("2. Cari Kontak"); | |
System.out.println("3. Edit Kontak"); | |
System.out.println("4. Hapus Kontak"); | |
System.out.println("5. EXIT"); | |
System.out.println("Pilih angka"); | |
// Input pilihan | |
int selected = inpObj.nextInt(); | |
// Menambahkan nomor baru | |
if(selected < 1 || selected > 6) { | |
System.out.println("Pilihan tidak valid !"); | |
} | |
if(selected == 1) { | |
System.out.println(); | |
System.out.println("Masukkan nama : "); | |
String unameBaru = inpObj.next(); | |
System.out.println("Masukkan nomor telepon : "); | |
String noTelpBaru = inpObj.next(); | |
contacts.put(hashFunction(unameBaru), noTelpBaru); | |
System.out.println(); | |
System.out.println("Kontak telah ditambahkan"); | |
System.out.println(); | |
} | |
else if(selected == 2) { | |
System.out.println(); | |
System.out.println("Masukkan nama kontak yang ingin dicari: "); | |
String unameCari = inpObj.next(); | |
boolean check = contacts.containsKey(hashFunction(unameCari)); | |
System.out.println(); | |
if(check) { | |
System.out.println("Kontak ditemukan"); | |
System.out.println(unameCari + "\t\t" + contacts.get(hashFunction(unameCari))); | |
} | |
else { | |
System.out.println("Kontak tidak ditemukan !"); | |
System.out.println("Pilih 1 untuk menambahkan kontak baru"); | |
} | |
System.out.println(); | |
} | |
else if(selected == 3) { | |
System.out.println(); | |
System.out.println("Masukkan nama kontak yang ingin diedit: "); | |
String unameEdit = inpObj.next(); | |
boolean check = contacts.containsKey(hashFunction(unameEdit)); | |
System.out.println(); | |
if(check) { | |
System.out.println("Masukkan nomor baru: "); | |
String noTelpEdit = inpObj.next(); | |
contacts.compute(hashFunction(unameEdit), (key, val) -> val = noTelpEdit); | |
System.out.println(); | |
System.out.println("Nomor telah diubah"); | |
} | |
else { | |
System.out.println("Kontak tidak ditemukan !"); | |
System.out.println("Pilih 1 untuk menambahkan kontak baru"); | |
} | |
System.out.println(); | |
} | |
else if(selected == 4) { | |
System.out.println(); | |
System.out.println("Masukkan nama kontak yang ingin dihapus: "); | |
String unameRemove = inpObj.next(); | |
boolean check = contacts.containsKey(hashFunction(unameRemove)); | |
System.out.println(); | |
if(check) { | |
contacts.remove(hashFunction(unameRemove)); | |
System.out.println("Kontak telah dihapus"); | |
} | |
else { | |
System.out.println("Kontak tidak ditemukan !"); | |
} | |
System.out.println(); | |
} | |
else if(selected == 5) { | |
inpObj.close(); | |
break; | |
} | |
} | |
} | |
} |
Dan berikut merupakan hasil output program
Komentar
Posting Komentar