【问题标题】:Overwrite the hash value (java)覆盖哈希值(java)
【发布时间】:2017-05-02 12:00:16
【问题描述】:

有人可以帮忙吗? :)

我没有通过这个 JUnit 测试:

@Test
public void testInsert() {
    Hashtable <Boolean> h = new Hashtable <Boolean> (1000, PROBE_TYPE.DOUBLE_HASH);
    for (int i = 0; i < 2000; i++) {
        for (int j = 2000; j > 0; j--) {
            h.put(i + ":" + j, true);
        }
    }
}

这是我的 put 方法:

对于 put 方法,必须存储针对给定键的值。如果loadFactor&gt;maxLoadresize()(调整数组大小的方法)。如果已经有键,则覆盖该值。新的Pair 项目包括(键,值)findEmpty(查找数组中的下一个空位置以存储该对)。调用findEmpty,将键的散列值作为搜索的开始位置,stepNum 为零,原始键。

public void put(String key, V value) {
    boolean isTrue = false;
    int size = 0;
    Pair aPair = new Pair(key, value);
    if (getLoadFactor() > maxLoad) { //if the maxLoad value is exceeded.
        resize(); //call the resize method.
    }
    if (hasKey(key)) { //if there is a key(position occupied).
        while (!isTrue) {
            if (size < max) { //if the size is less than the maximum size.
                if (arr[hash(key)].equals(key)) { //if the key already exists
                    aPair.value = value; //overwrite the value
                    isTrue = false;
                }
                size++;
            }
        }
    } else { //if the position is not occupied.
        int empty = findEmpty(hash(key), 0, key); //find the next empty position.
        arr[empty] = aPair; // stored in the empty position.
    }
    itemCount++;
}

Pair 实例被存储(在一个数组中)。如果发生碰撞,请检查原始密钥。这是Pair 类:

private class Pair {
    private String key;
    private V value;

    public Pair(String key, V value) {
        this.key = key;
        this.value = value;
    }
}

getLoadFactor():为大小返回一个双精度值
maxLoad:是一个双精度值 = 0.6
itemCount:存储在数组中的项目数
hasKey():返回一个布尔值true/false 是否有密钥
private V find(int startingPosition, String key, int stepNumber)
private int findEmpty(int startingPosition, int stepNumber, String key)
这是一个哈希表Hashtable&lt;V&gt; 我正在使用数组private Object[] arr

【问题讨论】:

  • resize() 函数中发生了什么?
  • 刚刚发布。
  • 您的问题是什么?需要写HashTable实现吗?
  • 如上所述,我未能通过 JUnit 测试:testInsert,我认为这是由 put 的方法错误引起的。你能帮我通过考试吗?
  • JUnit 肯定会给你一个堆栈跟踪吗?它说什么?它指的是您发布的代码的哪一行?

标签: java arrays junit hashtable hashcode


【解决方案1】:

我怀疑您的问题是您的 put 方法中的无限 while 循环。首先,只有当您将isTrue 设置为true 时,循环才会终止,而您永远不会这样做。将if 中的分配更改为isTrue = true; 可能会有所帮助,但前提是您进入那里。如果size 大于等于max,那么无论循环多少次你都不会,所以它仍然是无限的。接下来,如果正确理解arr 包含Pair 对象,arr[hash(key)].equals(key) 将永远不会为真,同时也可以防止循环终止。

那里可能还有更多错误。希望这能让你更进一步。

【讨论】:

    【解决方案2】:
    import java.util.Hashtable;
    
    
    public class Main <V>{
    
        private Integer max;
        private Object[] arr;
        private long itemCount = 0;
        private double maxLoad;
    
        public Main(int i) {
            this.max = i;
            maxLoad = 0.75;
            arr = new Object[i];
        }
    
        public static void main(String[] args) {
            Main<Boolean> h = new Main<Boolean>(1000);
            int counter = 0;
            for(int i=0;i<1000;i++) {
                for(int j=1000;j>0;j--) {
                    h.put(i+":"+j, true);
                    System.out.println(counter);
                    counter++;
                }
            }
        }
    
    
    
        private void resize() {
            Object[] oldArray = arr;
            max = max * 2;
            arr = new Object[max];
            itemCount = oldArray.length;
    
            for (int i = 0; i < oldArray.length; i++) {
                if (oldArray[i] != null) {
                    Pair arPair = (Pair) oldArray[i];
                    arr[i] = arPair;
                }
            }
        }
    
        public void put(String key, V value) {
            boolean isTrue = false;
            int size = 0;
            Pair aPair = new Pair(key, value);
            if (getLoadFactor() > maxLoad) { // if the maxLoad value is exceeded.
                resize(); // call the resize method.
            }
            int index = hasKey(key);
            if (index != -1) { // if there is a key(position occupied).
                ((Pair<V>)arr[index]).value = value;
            } else { // if the position is not occupied.
                int empty = findEmpty(hash(key), 0, key); // find the next empty
                                                            // position.
                arr[empty] = aPair; // stored in the empty position.
            }
            itemCount++;
        }
    
        private int findKey(String key) {
            int index = 0;
            for (Object obj  : arr) {
                Pair pair = (Pair) obj;
                if(pair!= null && pair.key.equals(key))
                    return index;
    
                index++;
            }
            return 0;
        }
    
        private double getLoadFactor() {
    
            return (double)itemCount / max;
        }
    
        private int findEmpty(int hash, int i, String key) {
            int j = 0 ;
            for (Object obj  : arr) {
                Pair pair = (Pair) obj;
                if(pair != null){
                    j++;    
                }else{
                    return j;
                }
    
            }
            return j;
        }
    
        private int hash(String key) {
            return key.hashCode();
        }
    
        private int hasKey(String key) {
            int counter = 0 ;
            for (Object obj  : arr) {
                Pair pair = (Pair) obj;
                if(pair != null && pair.key.equals(key)){
                    return counter;
                }
                counter++;
            }
            return -1;
        }
    
    
    
        private class Pair<V> {
            private String key;
            private V value;
    
            public Pair(String key, V value) {
                this.key = key;
                this.value = value;
            }
        }
    
    }
    

    【讨论】:

    • 我想添加一些评论。有些方法我没有,所以我编写了代码。 resize() 方法中还有一个递归调用,递归调用无限循环。
    • 这也不是 Hashtable 实现,算法太慢 1m 记录计数缓慢。我试图修复其他东西。
    • 感谢您的帮助,但是,如果密钥已经存在,那么它的值应该被覆盖。 resize() 方法没问题,我不需要任何东西。
    • 好的,你可以找到key的位置,然后替换值。我将在一分钟内编写该代码
    • 谢谢您,您是否看到任何其他会阻止测试通过的情况? :
    猜你喜欢
    • 1970-01-01
    • 2019-10-04
    • 2014-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-25
    • 1970-01-01
    相关资源
    最近更新 更多