/** * The simplest hashtable I could come up with. No generics. * The key must be an integer. The value must be a string. * It holds only 4 items. * * @author gtowell * Created: Sep 2020 * Modified: March 2021 */ public class SimpleHT { private class Pair { // the key. Once set it canot be changed public final Integer key; // the value public String value; //Create a key value pair. Pair(Integer ky, String val) { key = ky; value = val; } } /** the array in which the data will be stored */ private Pair[] backingArray; public SimpleHT() { backingArray = new Pair[4]; } private int h(int k) { return k%4; } public void put(Integer key, String value) { backingArray[h(key)] = new Pair(key, value); } public String get(Integer key) { return backingArray[h(key)].value; } public static void main(String[] args) { SimpleHT sht = new SimpleHT(); for (int i=0; i<10; i++) { System.out.println("adding item with key=" + i + " value=" + String.format("%c", 'a'+i)); sht.put(i, String.format("%c", 'a'+i)); } for (int i=0; i<10; i++) System.out.println("getting key="+i+" value="+sht.get(i)); } }