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 extends AbstractMap { private static class Entry implements Map.Entry { K key; V value; Entry next;

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 next) { this.key = key; this.value = value; this.next = next; } }

private final static int DEFAULT_CAPACITY = 5; private Entry[] table = new Entry[DEFAULT_CAPACITY]; private int size;

private int hashIndex (Object key) { int index = key.hashCode() % table.length; if (index < 0) index += table.length; return index; }

private Entry find (Object key) { int index = hashIndex(key); for (Entry node = table[index]; node != null; node = node.next) if (key.equals(node.key)) return node; return null; }

public boolean containsKey (Object key) { return find(key) != null; }

public V get (Object key) { Entry node = find(key); if (node == null) return null; return node.value; }

public V put (K key, V value) { Entry node = find(key); if (node != null) { V old = node.value; node.value = value; return old; } if (size == table.length) rehash(2 * table.length); int index = hashIndex(key); table[index] = new Entry(key, value, table[index]); size++; return null; } public V remove (Object key) { // IMPLEMENT // Get the index for the key. int index = hashIndex(key); // What if the linked list at that index is empty? if( table[index] == null){ return null; }

// 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 node = table[index]; Entry previous = node; while(node.next != null){ previous = node; node = node.next; if(node.key.equals(key)){ previous.next = node.next; size--; return node.value; } }

// Return null otherwise. return null; }

private void rehash (int newCapacity) { // IMPLEMENT Entry[] temp = table; table = new Entry[newCapacity]; for(int i=0; i node =temp[i]; K key; V value; while(node != null){ key = node.key; value = node.value; int index = key.hashCode() % table.length; if (index < 0) index += table.length; table[index] = new Entry(key, value, table[index]); node = node.next; } }

}

private Iterator> entryIterator () { return new EntryIterator(); }

private class EntryIterator implements Iterator> { // EXERCISE

public boolean hasNext () { // EXERCISE return false; }

public Map.Entry next () { // EXERCISE

return null; }

public void remove () {} }

public Set> entrySet() { return new EntrySet(); }

private class EntrySet extends AbstractSet> { public int size() { return size; }

public Iterator> iterator () { return entryIterator(); }

public void remove () {} }

public String toString () { String ret = "------------------------------ "; for (int i = 0; i < table.length; i++) { ret = ret + i + ":"; for (Entry node = table[i]; node != null; node = node.next) ret = ret + " " + node.key + " " + node.value; ret = ret + " "; } return ret; }

public static void main (String[] args) { ChainedHashTable table = new 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!