Created
August 29, 2020 14:06
-
-
Save SiAust/ced635cf142861e0edd45091d7f84d30 to your computer and use it in GitHub Desktop.
A simple example of HashTable in Java
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
| import java.util.Scanner; | |
| class Main { | |
| public static void main(String[] args) { | |
| Scanner sc = new Scanner(System.in); | |
| HashTable<String> table = new HashTable<>(Integer.parseInt(sc.nextLine())); | |
| while (sc.hasNext()) { | |
| String[] inputArr = sc.nextLine().split(" "); | |
| switch (inputArr[0]) { | |
| case "put" : | |
| table.put(Integer.parseInt(inputArr[1]), inputArr[2]); | |
| break; | |
| case "get" : | |
| System.out.println(table.get(Integer.parseInt(inputArr[1]))); | |
| break; | |
| case "remove" : | |
| table.remove(Integer.parseInt(inputArr[1])); | |
| break; | |
| } | |
| } | |
| } | |
| private static class TableEntry<T> { | |
| private final int key; | |
| private final T value; | |
| private boolean removed; | |
| public TableEntry(int key, T value) { | |
| this.key = key; | |
| this.value = value; | |
| } | |
| public int getKey() { | |
| return key; | |
| } | |
| public T getValue() { | |
| return value; | |
| } | |
| public void remove() { | |
| removed = true; | |
| } | |
| public boolean isRemoved() { | |
| return removed; | |
| } | |
| } | |
| private static class HashTable<T> { | |
| private int size; | |
| private TableEntry[] table; | |
| public HashTable(int size) { | |
| this.size = size; | |
| table = new TableEntry[size]; | |
| } | |
| public boolean put(int key, T value) { | |
| int index = findKey(key); | |
| if (index == -1) return false; | |
| table[index] = new TableEntry(key, value); | |
| return true; | |
| } | |
| public T get(int key) { | |
| int index = findKey(key); | |
| if (index == -1 || table[index] == null) return (T) "-1"; | |
| return (T) table[index].getValue(); | |
| } | |
| public void remove(int key) { | |
| int index = findKey(key); | |
| if (table[index] != null) { | |
| table[index].remove(); | |
| table[index] = new TableEntry(key, "-1"); | |
| } | |
| } | |
| private int findKey(int key) { | |
| int hash = key % size; | |
| // loop through table looking for empty index (hash) | |
| while (!(table[hash] == null || table[hash].getKey() == key)) { // while not null (empty) bucket/ | |
| // hash already index for correct key | |
| hash = (hash + 1) % size; | |
| // if we are at the end of the table size return false? | |
| if (hash == key % size || table[hash].isRemoved()) { // we've gone through all values of modulus | |
| // and are back at the original hash | |
| return -1; | |
| } | |
| } | |
| return hash; | |
| } | |
| @Override | |
| public String toString() { | |
| StringBuilder tableBuilder = new StringBuilder(); | |
| int i = 0; | |
| try { | |
| for (i = 0; i < table.length; i++) { | |
| if (table[i] != null) { | |
| tableBuilder.append(String.format("index %d: Key= %d Value= %s \n", i, | |
| table[i].getKey(), | |
| table[i].getValue())); | |
| } | |
| } | |
| } catch (Exception e) { | |
| e.printStackTrace(); | |
| System.out.println("i: " + i); | |
| } | |
| return tableBuilder.toString(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment