Question: This is Java. How do I set up EntryIterator here? import java.util.*; import javax.xml.soap.Node; public class ChainedHashTable extends AbstractMap { private static class Entry implements
This is Java. How do I set up EntryIterator here?
import java.util.*;
import javax.xml.soap.Node;
public class ChainedHashTable
public K getKey () { return key; } public V getValue () { return value; } public V setValue (V value) { return this.value = value; }
Entry (K key, V value, Entry
private final static int DEFAULT_CAPACITY = 5; private Entry
private int hashIndex (Object key) { int index = key.hashCode() % table.length; if (index < 0) index += table.length; return index; }
private Entry
public boolean containsKey (Object key) { return find(key) != null; }
public V get (Object key) { Entry
public V put (K key, V value) { Entry
// What if the first element of the list has this key? if( table[index].key.equals(key) ){ V old = table[index].value; table[index] = table[index].next; size--; return old; }
// If it is further down the list, make sure you keep track of // the pointer to the previous entry, because you will need to // change its next variable. Entry
// Return null otherwise. return null; }
private void rehash (int newCapacity) { // IMPLEMENT Entry
}
private Iterator
private class EntryIterator implements Iterator
public boolean hasNext () { // EXERCISE return false; }
public Map.Entry
return null; }
public void remove () {} }
public Set
private class EntrySet extends AbstractSet
public Iterator
public void remove () {} }
public String toString () { String ret = "------------------------------ "; for (int i = 0; i < table.length; i++) { ret = ret + i + ":"; for (Entry
public static void main (String[] args) { ChainedHashTable
table.put("Brad", 46); System.out.println(table); table.put("Hal", 10); System.out.println(table); table.put("Kyle", 6); System.out.println(table); table.put("Lisa", 43); System.out.println(table); table.put("Lynne", 43); System.out.println(table); table.put("Victor", 46); System.out.println(table); table.put("Zoe", 6); System.out.println(table); table.put("Zoran", 76); System.out.println(table);
for (String key : table.keySet()) System.out.print(key + " "); System.out.println();
table.remove("Zoe"); System.out.println(table); table.remove("Kyle"); System.out.println(table); table.remove("Brad"); System.out.println(table); table.remove("Zoran"); System.out.println(table); table.remove("Lisa"); System.out.println(table); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
