为哈希表编写迭代器

时间:2014-03-12 18:58:14

标签: java iterator hashtable

我想知道,在实现了一个哈希表后,我有一个List数组。我想知道如何实现密钥和值的Iterator方法。

这是我的尝试:

public Iterator<K> keys() {

    return new Iterator<K>(){

        int index = 0;

        @Override
        public boolean hasNext() {
            if (index >= mytable.length) return false;
                            else return true;

        }

        @Override
        public K next() {
            return mytable[index++]
        }

        @Override
        public void remove() {
            // TODO Auto-generated method stub

        }
    };

代码:

public class HashTable<K, V> {


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<Entry>[] 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 < 0) index += buckets.length;
    if (buckets[index] ==  null){ 
        //Create a new List at buckets[index]
        buckets[index] = (List<Entry>) 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 > (loadFactor * buckets.length)){
        rehash();
    }
}

private void rehash(){
    //Save reference to old buckets
    List<Entry>[] old = buckets;
    //Double capacity of this buckets
    buckets = new List[2*old.length + 1];

    //Reinsert
    numKeys = 0;
    numDeletes = 0;
    for (int index = 0; index < 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 < 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<E> keys() {

    return new Iterator<E>(){

        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<V> values() {

    return new Iterator<V>(){

        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

        }

    };
}

}

我知道我的迭代器是错误的。我想知道我是否能得到一些帮助。

感谢您的所有帮助,建议和建议。

0 个答案:

没有答案