Presentation is loading. Please wait.

Presentation is loading. Please wait.

Podcast Ch21d Title: Hash Class Iterators

Similar presentations


Presentation on theme: "Podcast Ch21d Title: Hash Class Iterators"— Presentation transcript:

1 Podcast Ch21d Title: Hash Class Iterators
Description: Overview; IteratorImpl; next() method; remove() method Participants: Barry Kurtz (instructor); John Helfert and Tobie Williams (students) Textbook: Data Structures for Java; William H. Ford and William R. Topp

2 Hash Class Iterators Search the hash table for the first nonempty bucket in the array of linked lists. Once the bucket is located, the iterator traverses all of the elements in the corresponding linked list and then continues the process by looking for the next nonempty bucket. The iterator reaches the end of the table when it reaches the end of the list for the last nonempty bucket.

3 Hash Class Iterators (continued)
Iterator objects are instances of the inner class IteratorImpl whose variables are: Integer index that identifies the current bucket (table[index]) scanned by the iterator. The Entry reference next pointing to the current node in the current bucket. The variable lastReturned that references the last value returned by next(). The iterator variable expectedModCount used in conjunction with the collection variable modCount.

4 Hash Class Iterators (continued)
// inner class that implements hash table iterators private class IteratorImpl implements Iterator<T> { // next entry to return Entry<T> next; // to check iterator consistency int expectedModCount; // index of current bucket int index; // reference to the last value returned by next() T lastReturned; . . . }

5 Hash Class Iterators (cont)
The elements enter the collection in the order (19, 32, 11, 27) using the identify hash function. The iterator visits the elements in the order (11, 32, 27, 19).

6 Hash Iterator Constructor
A loop iterates up the list of buckets until it locates the first nonempty bucket. The loop variable i becomes the initial value for index and table[i] references the front of the list. This is the initial value for next.

7 Hash Iterator Constructor (concluded)
IteratorImpl() { int i = 0; Entry<T> n = null; // the expected modCount starts at modCount expectedModCount = modCount; // find the first nonempty bucket if (hashTableSize != 0) while (i < table.length && ((n = table[i]) == null)) i++; next = n; index = i; lastReturned = null; }

8 Hash Iterator next() The method next() first determines that the operation is valid by checking that modCount and expectedModCount are equal and that we are not at the end of the hash table. If the iterator is in a consistent state, next() saves entry.value in lastReturned and uses a loop index i and entry to perform the iterator scan for the subsequent element in the hash table. The return value is lastReturned.

9 Hash Iterator next() (continued)
public T next() { // check for iterator consistency if (modCount != expectedModCount) throw new ConcurrentModificationException(); // we will return the value in Entry object next Entry<T> entry = next; // if entry is null, we are at the end if (entry == null) throw new NoSuchElementException(); // capture the value we will return lastReturned = entry.value; // move to the next entry in the current // linked list Entry<T> n = entry.next; // record the current bucket index int i = index;

10 Hash Iterator next() (concluded)
if (n == null) { // we are at the end of a bucket; search for // the next nonempty bucket i++; while (i < table.length && (n = table[i]) == null) } index = i; next = n; return lastReturned;

11 Hash Iterator remove()
The remove() method first determines that the operation is valid by checking that lastReturned is not null and that modCount and expectedModCount are equal. If all is well, the iterator remove() method calls the Hash class remove() method with lastReturned as the argument. By assigning to expectedModCount the current value of modCount, the iterator remains consistent.

12 Hash Iterator remove() (concluded)
public void remove() { // check for a missing call to next() or // previous() if (lastReturned == null) throw new IllegalStateException( "Iterator call to next() " + "required before calling remove()"); if (modCount != expectedModCount) throw new ConcurrentModificationException(); // remove lastReturned by calling remove() // in Hash; this call will increment modCount Hash.this.remove(lastReturned); expectedModCount = modCount; lastReturned = null; }


Download ppt "Podcast Ch21d Title: Hash Class Iterators"

Similar presentations


Ads by Google