覆盖哈希值(java)

时间:2016-12-16 16:28:52

标签: java arrays junit hashtable hashcode

有人可以在这提供一些帮助吗? :)

我无法通过此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>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<V>   我正在使用数组private Object[] arr

2 个答案:

答案 0 :(得分:0)

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;
        }
    }

}

答案 1 :(得分:0)

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

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