/**implements Map using a hashtable */ import java.util.Set; import maps.Map.Entry; public class HashTableMap,V> implements Map { private NodeSet>[] a; private int capacity; private int size; public class HashEntry,V> implements Entry{ /**Compares the specified object with this entry for equality*/ K key; V value; /**default Constructor*/ public HashEntry(){ key=null; value=null; } /**Constructor*/ public HashEntry(K k, V v){ key=k; value=v; } /**make two entries if they contain the same key and value*/ public boolean equals(Object o){ if(o==null)return false; else { if(getClass()!=o.getClass()) return false; else return ((((Entry) o).getKey().equals(key))&&(((Entry) o).getValue().equals(key))); } } /**Returns the key corresponding to * this entry*/ public K getKey(){return key;} /**Returns the value corresponding to this entry*/ public V getValue(){return value;} /**Returns the hash code value for this map entry * note hash code and hash function are compined */ public int hashCode(){ return key.hashCode(capacity); } /**Replaces the value corresponding to this entry with the specified value*/ public V setValue(V v){ V oldVal=value; value=v; return oldVal; } /**Returns string representation of entry*/ public String toString(){ return "[" + key + "," + value + "]"; } } /**Creates a new hash table with given size * empty set at each index*/ public HashTableMap(int n){ capacity=n; size=0; a= (NodeSet>[]) new NodeSet[capacity]; //compiler may warn, but ok for(int i=0;i>(); } } /**return the number of entries in the map*/ public int size(){return size;} /**returns true if this map contains no key-value mappings*/ public boolean isEmpty(){return size==0;} /** Returns the value to which the specific key is mapped, or null */ public V get(K key){ HashEntry entry=new HashEntry(key,null); int position=entry.hashCode(); V newVal=null; for(HashEntry entry2:a[position]) if(entry2.getKey().equals(entry.getKey())) newVal=entry2.getValue(); return newVal; } /** associates the specific value with the specified key in this map, return old value * or null if already an entry * with this key*/ public V put(K key, V value){ HashEntry entry=new HashEntry(key,value); int position=entry.hashCode(); System.out.println("want to place entry at position " + position); V newVal=null; if(a[position].isEmpty()) {a[position].add(entry);size++;} else{ for(HashEntry entry2:a[position]){ if(entry2.getKey().equals(entry.getKey())) { newVal=entry2.getValue();entry2.setValue(value);System.out.println("clash!");} } if(newVal==null) {a[position].add(entry);size++;} } return newVal; } /**Removes the mapping for a key from this map if present*/ public V remove(K key){ HashEntry entry=new HashEntry(key,null); int position=entry.hashCode(); HashEntry entry3=null; V newVal=null; for(HashEntry entry2:a[position]) if(entry2.getKey().equals(entry.getKey())) {entry3=entry2;newVal=entry2.getValue();} if(entry3!=null) {a[position].remove(entry3);size--;} return newVal; } /**return a set containing all the keys stored in map*/ public Set keys(){ Set mySet=(Set) new NodeSet(); for(int i=0;i entry2:a[i]) mySet.add(entry2.getKey()); return mySet; } /**return a set containing all the values associated with the values stored*/ public Set values(){ Set mySet=(Set) new NodeSet(); for(int i=0;i entry2:a[i]) mySet.add(entry2.getValue()); return mySet; } /**return a set containing all the key-value entries*/ public Set> entries(){ Set> mySet = (Set>) new NodeSet>(); for(int i=0;i entry2:a[i]) mySet.add(entry2); return mySet; } /** Return string representing the entries contained in the map*/ public String toString(){ Set> mySet = (Set>) this.entries(); String s=""; for(Entry entry: mySet) s=s+entry+ ","; return s; } /**Return string representing the entries of map with one row per index * of array - for debugging */ public String outputString(){ String s=""; for(int i=0;i entry2:a[i]) s=s+entry2+","; s=s+"\n"; } return s; } }