import java.math.BigInteger; import java.util.ArrayList; /** * A fairly basic implementation of a spparate chanining hashtable * @param the tpe of key * @param the type of value * Implements full separate chaining, but not rehashing. * Similarly, the size of the underlying table, once it is created, cannot * be changed. * @author gtowell * Created: April 25, 2020 * Modified: Sep 23, 2020 to use ArrayList */ public class SepChainHT { protected class Pair { /** The key, cannot be changed */ final L key; /** The value. It can be changed as a second put with the key * will change the value */ W value; /** * Initialize the node */ public Pair(L ll, W ww) { key=ll; value=ww; } /** Print the node, and all subsequent nodes in the linked list */ public String toString() { return "<"+key+", "+value+">"; } } /** The array holding the hashtable data. Yes, this is an array * of array lists */ private ArrayList>[] backingArray; /** The default size of the backing array */ private static int DEFAULT_CAPACITY = 1009; /** For polynomial accumulation, the multiplier. */ static int POLY_MULT=33; /** Default initialization */ public SepChainHT() { this(DEFAULT_CAPACITY); } /** * Initialize a hashtable of the given size * @param size the size of the hashtable to create */ @SuppressWarnings("unchecked") public SepChainHT(int size) { backingArray = new ArrayList[size]; } /** * Implemets polynomical accumulation on strings. * Since every object can be translated into a string This can be run * on an arbitrary object with no loss of generality. * @param ss the string to generate a hash value for * @return the hash value */ public int stringHasher(String ss) { BigInteger ll = new BigInteger("0"); for (int i=0; i newPair = new Pair<>(key, value); if (backingArray[loc]==null) { backingArray[loc] = new ArrayList<>(); backingArray[loc].add(newPair); } else { ArrayList> arrLis = backingArray[loc]; Pair foundPair = null; for (Pair pr : arrLis) { if (pr.key.equals(key)) { foundPair=pr; break; } } if (foundPair==null) { // key is not in the list arrLis.add(newPair); } else { foundPair.value = value; } } } /** * Get the value stored in the hashtable given the key. * @param key the key * @return the value associated with the key */ public V get(K key) { int loc = stringHasher(key.toString()); if (backingArray[loc]==null) { return null; } ArrayList> arrLis = backingArray[loc]; for (Pair pr : arrLis) { if (pr.key.equals(key)) { return pr.value; } } return null; } /** * The number of distinct keys in the hshtable. * @return The number of distinct keys in the hashtable */ public int size() { int count=0; for (int i=0; i> arrLis = backingArray[i]; for (Pair pr : arrLis) { sb.append(" " + pr.toString()); } } } return sb.toString(); } public static void main(String[] args) { SepChainHT sht = new SepChainHT<>(131); try { BookReader br = new BookReader("ham.txt"); for (int i=0; i<500; i++) { String s = br.nextWord(); sht.put(s,s); } } catch (Exception ee) { System.out.println(ee); } System.out.println(sht.size()); System.out.println(sht); //System.out.println(sht.stringHasher("abcdefabcdefgabcdefgh")); } }