Implementasi HashTable Pada Java
Hash Table
Hash table merupakan salah satu struktur data yang terdiri dari serangkaian key value pair. Hash table menerapkan fungsi hash khusus untuk menghasilkan suatu index yang unik.
Implementasi Hash Table Pada Java
Berikut ini merupakan implementasi Hash table pada java
Source code
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
package assignment_11; | |
import java.util.Hashtable; | |
/** | |
* This class represents Phone Book | |
* that contains Map of name and corresponding phone number | |
* | |
* @author Fajar Zuhri Hadiyanto | |
* @version 1.0 | |
* @since June 20th 2021 | |
* */ | |
public class PhoneBook { | |
/** Field that represents map of hashed name and corresponding phone number */ | |
private final Hashtable<Integer, PhoneNumber> hashtable; | |
/** | |
* This constructor will create new instance of {@link Hashtable} | |
* */ | |
public PhoneBook() { | |
this.hashtable = new Hashtable<>(); | |
} | |
/** | |
* This method is used to hash given key with a certain algorithm | |
* and return the hashed key | |
* | |
* @param key key to be hashed | |
* @return hashed key | |
* */ | |
private int hashFunction(String key) { | |
int hash = 97; | |
for (char ch: key.toCharArray()) { | |
hash = (19 * hash + ch) % Integer.MAX_VALUE; | |
} | |
return hash; | |
} | |
/** | |
* This method is used to check if this phone book | |
* include key in it | |
* | |
* @param key key to be looked for | |
* @return true if key found, else false | |
* */ | |
public boolean hasKey(String key) { | |
return hashtable.containsKey(this.hashFunction(key)); | |
} | |
/** | |
* This method is used to put a new person and phone number pair | |
* into this phone book | |
* | |
* @param key person's name as a key | |
* @param phone phone number | |
* @return true if key doesn't exist and inserted successfully, else false | |
* */ | |
public boolean put(String key, PhoneNumber phone) { | |
if(this.hasKey(key)) return false; | |
this.hashtable.put(this.hashFunction(key), phone); | |
return true; | |
} | |
/** | |
* This method is used to get a phone number by given person's name | |
* | |
* @param key person's name as a key | |
* @return phone number | |
* */ | |
public PhoneNumber getByKey(String key) { | |
return this.hashtable.get(this.hashFunction(key)); | |
} | |
/** | |
* This method is used to edit a phone number by given person's name | |
* | |
* @param key person's name as a key | |
* @param phone new phone number | |
* @return true if key exist and it's phone number edited successfully, else false | |
* */ | |
public boolean editByKey(String key, PhoneNumber phone) { | |
if(!this.hasKey(key)) return false; | |
this.hashtable.replace(this.hashFunction(key), phone); | |
return true; | |
} | |
/** | |
* This method is used to removed a data by given person's name | |
* | |
* @param key person's name as a key | |
* @return true if data removed successfully, else false | |
* */ | |
public boolean removeByKey(String key) { | |
return this.hashtable.remove(this.hashFunction(key)) != null; | |
} | |
} |
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
package assignment_11; | |
/** | |
* This class represents Phone number | |
* that contains phone code and the actual number | |
* | |
* @author Fajar Zuhri Hadiyanto | |
* @version 1.0 | |
* @since June 20th 2021 | |
* */ | |
public class PhoneNumber { | |
/** Field that represents phone code for the number */ | |
int phoneCode; | |
/** Field that represents actual phone number of the number*/ | |
String phoneNumber; | |
/** | |
* This constructor will create new PhoneNumber with a given parameter | |
* | |
* @param phoneCode phone code | |
* @param phoneNumber phone actual number | |
* */ | |
public PhoneNumber(int phoneCode, String phoneNumber) { | |
this.phoneCode = phoneCode; | |
this.phoneNumber = phoneNumber; | |
} | |
/** | |
* This method will return string representation of this phone number | |
* by concatenating plus sign, phone code, additional space, and the actual number | |
* | |
* @return string representation of this phone number | |
* */ | |
@Override | |
public String toString() { | |
return "+" + this.phoneCode + " " + this.phoneNumber; | |
} | |
} |
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
package assignment_11; | |
import javafx.util.Pair; | |
import java.util.*; | |
/** | |
* This class represents application that show how | |
* {@link Hashtable} is implemented | |
* | |
* @author Fajar Zuhri Hadiyanto | |
* @version 1.0 | |
* @since June 20th 2021 | |
* */ | |
public class MainApplication { | |
/** | |
* Static field to contain all function with {@link Integer} key | |
* to represent function order in the app menu, then {@link Pair} | |
* of {@link String} and {@link Runnable} as a representation of function | |
* */ | |
private static HashMap<Integer, Pair<String, Runnable>> functions; | |
/** Static field to contain the main data of this application */ | |
private static PhoneBook data; | |
/** | |
* This method is used as main method | |
* | |
* @param args arguments to the console app while compiled and launched. | |
* */ | |
public static void main(String[] args) { | |
initApp(); | |
programMenu(); | |
} | |
/** | |
* This method is used to initialize the app | |
* by initialize the main data and configure all | |
* possible function to be executed in the app | |
* */ | |
private static void initApp() { | |
data = new PhoneBook(); | |
functions = new HashMap<>(); | |
functions.put(1, new Pair<>("Add data", MainApplication::addData)); | |
functions.put(2, new Pair<>("Check number", MainApplication::checkNumber)); | |
functions.put(3, new Pair<>("Edit data", MainApplication::editData)); | |
functions.put(4, new Pair<>("Remove data", MainApplication::removeData)); | |
functions.put(5, new Pair<>("Exit application", MainApplication::programExit)); | |
} | |
/** | |
* This is a utility method to get a new {@link Scanner} object | |
* | |
* @return new {@link Scanner} object | |
* */ | |
private static Scanner scanner() { | |
return new Scanner(System.in); | |
} | |
/** | |
* This is a utility method to prompt an integer data and return that value. | |
* This method will reprompt if the input is not an integer. | |
* | |
* @param message prompt message to be shown | |
* @return int data inputted by user | |
* */ | |
private static int promptInt(String message) { | |
int value; | |
while(true) { | |
System.out.print(message); | |
try { | |
value = scanner().nextInt(); | |
return value; | |
} catch(InputMismatchException e) { | |
System.out.println("Input must be an integer!"); | |
} | |
} | |
} | |
/** | |
* This is a utility method to prompt a string data and return that value | |
* | |
* @param message prompt message to be shown | |
* @return string data inputted by user | |
* */ | |
private static String promptString(String message) { | |
System.out.print(message); | |
return scanner().nextLine(); | |
} | |
/** | |
* This method is used to check if data exist by person's name. | |
* If data exist, show name and phone number | |
* */ | |
private static void checkNumber() { | |
String name = promptString("Enter name to be looked for the number : "); | |
PhoneNumber phone = data.getByKey(name); | |
if (phone != null) { | |
System.out.println("DATA FOUND"); | |
System.out.println("Name : " + name); | |
System.out.println("Phone number : " + phone); | |
} else { | |
System.out.println("DATA NOT FOUND !!!"); | |
} | |
} | |
/** | |
* This method is used to add new data to the phone book | |
* */ | |
private static void addData() { | |
String name = promptString("Enter name : "); | |
int phoneCode = promptInt("Enter phone code : "); | |
String phoneNumber = promptString("Enter phone number : "); | |
PhoneNumber phone = new PhoneNumber(phoneCode, phoneNumber); | |
if (data.put(name, phone)) { | |
System.out.println("Data added successfully "); | |
System.out.println("Name : " + name); | |
System.out.println("Phone number : " + phone); | |
} else { | |
System.out.println("ADDITION FAILED !!!"); | |
} | |
} | |
/** | |
* This method is used to edit phone number from phone book | |
* by given name | |
* */ | |
private static void editData() { | |
String name = promptString("Enter name to be edited the phone number : "); | |
if (data.hasKey(name)) { | |
int newPhoneCode = promptInt("Enter new phone code : "); | |
String newPhoneNumber = promptString("Enter new phone number : "); | |
PhoneNumber phone = new PhoneNumber(newPhoneCode, newPhoneNumber); | |
data.editByKey(name, phone); | |
} else { | |
System.out.println("DATA NOT FOUND !!!"); | |
} | |
} | |
/** | |
* This method is used to remove data from phone book | |
* by given name | |
* */ | |
private static void removeData() { | |
String name = promptString("Enter name to be removed :"); | |
if (data.removeByKey(name)) { | |
System.out.println("DATA REMOVED SUCCESSFULLY"); | |
} else { | |
System.out.println("DATA NOT FOUND !!!"); | |
} | |
} | |
/** | |
* This method is used to exit the application. | |
* */ | |
private static void programExit() { | |
System.exit(0); | |
} | |
/** | |
* This method is used to display the program menu. | |
* */ | |
private static void programMenu() { | |
System.out.println("==========| PROGRAM MENU |=========="); | |
for (Map.Entry<Integer, Pair<String, Runnable>> fun: functions.entrySet()) { | |
System.out.println(fun.getKey() + ". " + fun.getValue().getKey()); | |
} | |
programSwitcher(); | |
} | |
/** | |
* This method is used to get menu index inputted by user | |
* then, run the corresponding function. | |
* */ | |
private static void programSwitcher() { | |
int indexMenu = promptInt("Choose Menu [1 - 5]: "); | |
if (1 <= indexMenu && indexMenu <= 5) | |
functions.get(indexMenu).getValue().run(); | |
programMenu(); | |
} | |
} |
Hasil
1. Insert Data
2. Get Data
3. Update Data
4. Remove Data
5. Exit program
Source code dari program di atas dapat diakses di sini
Comments
Post a Comment