Hashtable - Java
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.
Berikut adalah Source Code dan hasil dari percobaan:
This file contains 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
import java.util.HashMap; | |
import java.util.Scanner; | |
public class Apps { | |
HashMap<String, String> contact = new HashMap<>(); | |
public void getAllContact() { | |
contact.forEach((key, value) -> System.out.println("Nama: " + key + "\nNomor: " + value)); | |
} | |
public void addContact() { | |
System.out.print("Masukan nama: "); | |
String nama = scanner().nextLine(); | |
if (contact.containsKey(nama)){ | |
System.out.println("Contact Sudah ada\n"); | |
}else { | |
System.out.print("Masukan nomor: "); | |
String nomor = scanner().nextLine(); | |
contact.put(nama, nomor); | |
System.out.println("Contact ditambahkan\n"); | |
} | |
} | |
public void getContact() { | |
System.out.print("Nama: "); | |
String nama = scanner().nextLine(); | |
if (contact.containsKey(nama)){ | |
System.out.println("Nomor: " + contact.get(nama)); | |
}else { | |
System.out.println("Contact tidak ditemukan\n"); | |
} | |
} | |
public void editContact() { | |
System.out.print("Nama: "); | |
String nama = scanner().nextLine(); | |
if (contact.containsKey(nama)){ | |
System.out.print("Nomor baru:"); | |
String nomor = scanner().nextLine(); | |
contact.replace(nama, nomor); | |
System.out.println("Contact berhasil diedit\n"); | |
}else { | |
System.out.println("Contact tidak ditemukan\n"); | |
} | |
} | |
public void removeContact() { | |
System.out.print("Nama: "); | |
String nama = scanner().nextLine(); | |
if (contact.containsKey(nama)){ | |
contact.remove(nama); | |
System.out.println("Contact berhasil dihapus\n"); | |
}else { | |
System.out.println("Contact tidak ditemukan\n"); | |
} | |
} | |
public Scanner scanner() { | |
return new Scanner(System.in); | |
} | |
} |
This file contains 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
import java.util.Scanner; | |
public class Main { | |
public static void main(String[] args) { | |
Scanner scanner = new Scanner(System.in); | |
Apps apps = new Apps(); | |
Boolean check = true; | |
while (check){ | |
System.out.println("MENU"); | |
System.out.println("1. Tambah Contact"); | |
System.out.println("2. List Contact"); | |
System.out.println("3. Lihat Contact"); | |
System.out.println("4. Ubah Contact"); | |
System.out.println("5. Hapus Contact"); | |
System.out.println("0. Keluar"); | |
System.out.print("Pilih menu"); | |
Integer menu = scanner.nextInt(); | |
switch (menu){ | |
case 1: | |
apps.addContact(); | |
break; | |
case 2: | |
apps.getAllContact(); | |
break; | |
case 3: | |
apps.getContact(); | |
break; | |
case 4: | |
apps.editContact(); | |
break; | |
case 5: | |
apps.removeContact(); | |
break; | |
case 0: | |
System.out.println("Keluar dari program!!"); | |
check = false; | |
break; | |
} | |
} | |
} | |
} |
Belum ada Komentar untuk "Hashtable - Java"
Posting Komentar