Hashtable rehash条件不评估Java

时间:2014-11-02 03:22:11

标签: java

我正在设计一个具有线性探测碰撞分辨率的Hashtable。我设计了一个容量来定义桶的数量和负载因子。当散列表中的元素数超过容量和负载因子的乘积时,应该重新散列数组。出于某种原因,甚至在我的代码中都没有调用该方法。这是我的主要方法:

public class MyJHUHashMap<K extends Comparable<? super K>, V> implements JHUHashMap<K, V>{

    private int cap;
    private int numElements;
    private double loadFactor;
    private Node<K,V>[] hashtable;

    public static void main(String[] args) {
        MyJHUHashMap<Integer, Integer> m = new MyJHUHashMap<>();
        m.put(1, 1);
        m.put(2, 2);
        m.put(3, 3);
        m.put(4, 4);
        m.put(5, 5);
        m.put(6, 6);
        m.put(7, 7);
        System.out.println(m.cap);
        System.out.println(m.size());
        for(int i=0; i<m.hashtable.length; i++) {
            if(m.hashtable[i] != null)
                System.out.println(m.hashtable[i].key);
        }
    }

这是我的代码:

private final class Node<K extends Comparable<? super K>, V>  {
        public K key;
        public V val;

        public Node() {
            this.key = null;
            this.val = null;
        }

        public Node(K theKey, V theValue) {
            this.key = theKey;
            this.val = theValue;
        }
    } 

    public MyJHUHashMap() {
        this.cap = 11;
        this.numElements = 0;
        this.loadFactor = 0.5;
        this.hashtable = new Node[cap];
    }

    public MyJHUHashMap(int pInitCap, double pLoadFactor) {
        if(pInitCap > 0 && pLoadFactor > 0) {
            this.cap = pInitCap;
            this.loadFactor = pLoadFactor;
        } else {
            throw new IllegalArgumentException();
        }
        this.numElements = 0;
        this.hashtable = new Node[cap];
    }

    @Override
    public V put(K key, V value) {
        if(containsKey(key)) {
            int foundindex = find(key);
            V prevObject = this.hashtable[foundindex].val;
            //hashtable[foundindex] = new Node(key, value);
            this.hashtable[foundindex].val = value;
            return prevObject;
        }
        int index = key.hashCode() % cap;
        if(this.hashtable[index] == null) {
            this.hashtable[index] = new Node<K,V>(key, value);
            this.numElements++;
            return null;
        } 
        boolean found = false;
        //index += 1;
        while(found) {
            index += 1;
            if(hashtable[index] == null) {
                hashtable[index] = new Node<K,V>(key, value);
                this.numElements++;
                found = true;
                //return null;
            }
            //index += 1;
        }
        if(this.numElements > this.cap * this.loadFactor) {
            System.out.println("Rehashing...");
            rehashAndResize();
        }
        return null;
    }

    public void rehashAndResize() {
        //Node<K,V>[] temp = new Node[this.hashtable.length*2];
        //Node<K,V>[] temp = new Node[this.hashtable.length*2];
        System.out.println("Calling rehash method");
        MyJHUHashMap<K,V> temp = new MyJHUHashMap<K, V>(this.hashtable.length * 2, this.loadFactor);
        for(int i=0; i<this.hashtable.length; i++) {
            temp.put(this.hashtable[i].key, this.hashtable[i].val);
        }

        this.hashtable = temp.hashtable;
    }

&#34;重散列&#34;和#34;调用rehash方法&#34;没有打印,所以这个方法没有被调用,我无法理解为什么。我输入了7个元素,当我向它询问元素的数量时,哈希表正确打印出来7,那么为什么不评估条件?

编辑:好的,经过仔细检查后,我发现重新连接方法从未到达,因为该方法提前返回。谢谢你的帮助!

0 个答案:

没有答案