title: Writing an Iterator for hashtable
body: <p>I was wondering , after implementing a hashtable where I have an array of List . I was wondering How I would implement an Iterator method for key and value.</p>
  
  <p>Here's my attempt:</p>
  
  <pre><code>public Iterator&lt;K&gt; keys() {
  
      return new Iterator&lt;K&gt;(){
  
          int index = 0;
  
          @Override
          public boolean hasNext() {
              if (index &gt;= mytable.length) return false;
                              else return true;
  
          }
  
          @Override
          public K next() {
              return mytable[index++]
          }
  
          @Override
          public void remove() {
              // TODO Auto-generated method stub
  
          }
      };
  </code></pre>
  
  <hr>
  
  <p>code:</p>
  
  <pre><code>public class HashTable&lt;K, V&gt; {
  
  
  class Entry {
  
      private k key;
      private V value;
  
      public Entry(K key, V value){
          this.key = key;
          this.value = value;
  
      } 
  
      public K getKey(){
          return this.key;
  
      }
  
      public V getValue(){
          return this.value;
      }
  
      public V setValue(V val){
          V old = value;
          value = val;
          return old;
  
      }
  }
  
  private double loadFactor = 0.75;
  private final int defaultSize = 64;
  private List&lt;Entry&gt;[] buckets; 
  private int numKeys = 0;
  private int numDeletes = 0;
  
  
  
  public HashTable() { 
      buckets = new List[defaultSize];
  
  
  }
  public void put( K key, V value ) {
  
      if (key == null || value == null) {
          throw new NullPointerException();
      }
  
      int index = key.hashCode() % buckets.length;
      if (index &lt; 0) index += buckets.length;
      if (buckets[index] ==  null){ 
          //Create a new List at buckets[index]
          buckets[index] = (List&lt;Entry&gt;) new Entry(key, value);
      }
  
      for (Entry nextItem : buckets[index]){
          if (nextItem.key.equals(key)){
              V oldVal = nextItem.value;
              nextItem.setValue(value);
  
          }
      }
      //Key is not in buckets, add new item
      buckets[index].add(new Entry(key,value));
      numKeys++;
      if(numKeys &gt; (loadFactor * buckets.length)){
          rehash();
      }
  }
  
  private void rehash(){
      //Save reference to old buckets
      List&lt;Entry&gt;[] old = buckets;
      //Double capacity of this buckets
      buckets = new List[2*old.length + 1];
  
      //Reinsert
      numKeys = 0;
      numDeletes = 0;
      for (int index = 0; index &lt; old.length; index ++) {
          if (old[index] != null){
              //Insert entry on expanded buckets
  
              put((old[index].get(index).key), old[index].get(index).value);
  
          }
      }
  
  }
  
  
  public V get( E key ) {
  
      int index = key.hashCode() % buckets.length;
      if (index &lt; 0) index += buckets.length;
      //If key is not in the table
      if (buckets[index] == null) return null;
  
      //Search the list at buckets[index] to find the key
      for(Entry nextItem: buckets[index]){
          if (nextItem.key.equals(key)){
              return nextItem.value;
          }
  
      }
      //Key is not in table
      return null;
  }
  
  public Iterator&lt;E&gt; keys() {
  
      return new Iterator&lt;E&gt;(){
  
          int index = 0;
  
          @Override
          public boolean hasNext() {
              return buckets[index].get(index).key != null;
  
  
          }
  
          @Override
          public E next() {
              return buckets[index ++].get(index).key;
          }
  
          @Override
          public void remove() {
              // TODO Auto-generated method stub
  
          }
      };
  }
  
  public Iterator&lt;V&gt; values() {
  
      return new Iterator&lt;V&gt;(){
  
          int index = 0;
  
          @Override
          public boolean hasNext() {
              return buckets[index].get(index).value !=null;
          }
  
          @Override
          public V next() {
              return buckets[index ++].get(index).value;
          }
  
          @Override
          public void remove() {
              // TODO Auto-generated method stub
  
          }
  
      };
  }
  </code></pre>
  
  <p>}</p>
  
  <p>I know my Iterators are wrong. I was wondering if I could get some help.</p>
  
  <p>Thanks for all help, suggestions, and advice.</p>