This is Java. How do I set up EntryIterator here? import java.util.*; import jav
ID: 3820926 • Letter: T
Question
This is Java. How do I set up EntryIterator here?
import java.util.*;
import javax.xml.soap.Node;
public class ChainedHashTable<K, V> extends AbstractMap<K, V> {
private static class Entry<K, V> implements Map.Entry<K, V> {
K key;
V value;
Entry<K,V> 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<K,V> next) {
this.key = key;
this.value = value;
this.next = next;
}
}
private final static int DEFAULT_CAPACITY = 5;
private Entry<K,V>[] 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<K,V> find (Object key) {
int index = hashIndex(key);
for (Entry<K,V> 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<K,V> node = find(key);
if (node == null)
return null;
return node.value;
}
public V put (K key, V value) {
Entry<K,V> 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<K,V>(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<K,V> node = table[index];
Entry<K,V> 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<K,V>[] temp = table;
table = new Entry[newCapacity];
for(int i=0; i<temp.length; i++){
Entry<K,V> 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<K,V>(key, value, table[index]);
node = node.next;
}
}
}
private Iterator<Map.Entry<K, V>> entryIterator () {
return new EntryIterator();
}
private class EntryIterator implements Iterator<Map.Entry<K, V>> {
// EXERCISE
public boolean hasNext () {
// EXERCISE
return false;
}
public Map.Entry<K, V> next () {
// EXERCISE
return null;
}
public void remove () {}
}
public Set<Map.Entry<K,V>> entrySet() { return new EntrySet(); }
private class EntrySet extends AbstractSet<Map.Entry<K, V>> {
public int size() { return size; }
public Iterator<Map.Entry<K, V>> iterator () {
return entryIterator();
}
public void remove () {}
}
public String toString () {
String ret = "------------------------------ ";
for (int i = 0; i < table.length; i++) {
ret = ret + i + ":";
for (Entry<K,V> 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<String, Integer> table =
new ChainedHashTable<String, Integer>();
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);
}
}
Explanation / Answer
import java.util.*;
import javax.xml.soap.Node;
public class ChainedHashTable<K, V> extends AbstractMap<K, V> {
private static class Entry<K, V> implements Map.Entry<K, V> {
K key;
V value;
Entry<K,V> 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<K,V> next) {
this.key = key;
this.value = value;
this.next = next;
}
}
private final static int DEFAULT_CAPACITY = 5;
private Entry<K,V>[] 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<K,V> find (Object key) {
int index = hashIndex(key);
for (Entry<K,V> 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<K,V> node = find(key);
if (node == null)
return null;
return node.value;
}
public V put (K key, V value) {
Entry<K,V> 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<K,V>(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<K,V> node = table[index];
Entry<K,V> 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<K,V>[] temp = table;
table = new Entry[newCapacity];
for(int i=0; i<temp.length; i++){
Entry<K,V> 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<K,V>(key, value, table[index]);
node = node.next;
}
}
}
private Iterator<Map.Entry<K, V>> entryIterator () {
return new EntryIterator();
}
private class EntryIterator implements Iterator<Map.Entry<K, V>> {
// EXERCISE
Entry<?,?>[] table = ChainedHashTable.this.table;
int index = table.length;
Entry<?,?> entry;
Entry<?,?> lastReturned;
int type;
public boolean hasNext () {
// EXERCISE
Entry<?,?> e = entry;
int i = index;
Entry<?,?>[] t = table;
/* Use locals for faster loop iteration */
while (e == null && i > 0) {
e = t[--i];
}
entry = e;
index = i;
return e != null;
}
public Map.Entry<K, V> next () {
Entry<?,?> et = entry;
int i = index;
Entry<?,?>[] t = table;
/* Use locals for faster loop iteration */
while (et == null && i > 0) {
et = t[--i];
}
entry = et;
index = i;
if (et != null) {
Entry<?,?> e = lastReturned = entry;
entry = e.next;
//return type == KEYS ? (Map)e.key : (type == VALUES ? (Map)e.value : (Map)e);
return (java.util.Map.Entry<K, V>) e;
}
return null;
}
public void remove () {}
}
public Set<Map.Entry<K,V>> entrySet() { return new EntrySet(); }
private class EntrySet extends AbstractSet<Map.Entry<K, V>> {
public int size() { return size; }
public Iterator<Map.Entry<K, V>> iterator () {
return entryIterator();
}
public void remove () {}
}
public String toString () {
String ret = "------------------------------ ";
for (int i = 0; i < table.length; i++) {
ret = ret + i + ":";
for (Entry<K,V> 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<String, Integer> table =
new ChainedHashTable<String, Integer>();
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);
Iterator it= table.entryIterator();
while(it.hasNext()){
Entry<String, Integer> et = (Entry<String, Integer>) it.next();
System.out.println(et.getKey()+" "+et.getValue());
}
}
}
----------output---------
------------------------------
0:
1:
2: Brad 46
3:
4:
------------------------------
0:
1:
2: Hal 10 Brad 46
3:
4:
------------------------------
0: Kyle 6
1:
2: Hal 10 Brad 46
3:
4:
------------------------------
0: Kyle 6
1:
2: Hal 10 Brad 46
3: Lisa 43
4:
------------------------------
0: Kyle 6
1:
2: Hal 10 Brad 46
3: Lynne 43 Lisa 43
4:
------------------------------
0:
1:
2:
3: Lisa 43
4:
5: Victor 46 Kyle 6
6:
7: Brad 46 Hal 10
8: Lynne 43
9:
------------------------------
0:
1:
2: Zoe 6
3: Lisa 43
4:
5: Victor 46 Kyle 6
6:
7: Brad 46 Hal 10
8: Lynne 43
9:
------------------------------
0:
1:
2: Zoran 76 Zoe 6
3: Lisa 43
4:
5: Victor 46 Kyle 6
6:
7: Brad 46 Hal 10
8: Lynne 43
9:
Lynne Brad Hal Victor Kyle Lisa Zoran Zoe
------------------------------
0:
1:
2: Zoran 76
3: Lisa 43
4:
5: Victor 46 Kyle 6
6:
7: Brad 46 Hal 10
8: Lynne 43
9:
------------------------------
0:
1:
2: Zoran 76
3: Lisa 43
4:
5: Victor 46
6:
7: Brad 46 Hal 10
8: Lynne 43
9:
------------------------------
0:
1:
2: Zoran 76
3: Lisa 43
4:
5: Victor 46
6:
7: Hal 10
8: Lynne 43
9:
------------------------------
0:
1:
2:
3: Lisa 43
4:
5: Victor 46
6:
7: Hal 10
8: Lynne 43
9:
------------------------------
0:
1:
2:
3:
4:
5: Victor 46
6:
7: Hal 10
8: Lynne 43
9:
Lynne 43
Hal 10
Victor 46
------------output---------
Note: We have implemented the hasNext and next method and used it in the program to iterate over the hash table.
Please feel free to as ask any question. Happy to help. God bless you!!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.