HashTable

Run Settings
LanguageJava
Language Version
Run Command
class Main { public static void main(String[] args) { HashTable<Integer> myHashTable = new HashTable<>(50); myHashTable.set("grapes", 10000); System.out.println("Printing grapes value: " + myHashTable.get("grapes")); } }
class HashTable<T> { private final T[] data; public HashTable(int size) { this.data = (T[]) new Object[size]; } private int hash(String key) { int hash = 0; for (int i = 0; i < key.length(); i++) { hash = (hash + Character.codePointAt(key, i) * i) % data.length; } return hash; } public void set(String key, T value) { int index = hash(key); this.data[index] = value; } public T get(String key) { int index = hash(key); return data[index]; } }
Editor Settings
Theme
Key bindings
Full width
Lines