使用并行数组编写简单的String hashMap。重新散列后获取某些键的空值

时间:2018-05-01 16:44:58

标签: java arrays hashmap

我第一次在put方法中获得冲突,即当hasKey返回-1时,rehashing方法启动,触发冲突的值变为doubled数组,可能为空插槽。但是System.out.println(m.get("1000"));给了我一些键的空值,这意味着它们丢失了。我不明白它们是如何丢失的,因为在keyArray中没有任何内容可以覆盖它们。

import java.util.*;

public class StringMapParallel implements Iterable<String>{

    private int nButckets = 2000;
    private String[] keyArray = new String[nButckets];
    private String[] valueArray = new String[nButckets];
    private int numberOfEntries = 0;

    public static void main(String[] args) {


        StringMapParallel m = new StringMapParallel();
;
        for (int i = 0; i < 8000; i++) {
            m.put(String.valueOf(i), String.valueOf(i));
        }
        System.out.println(m.get("1000"));


    }



    private void rehashing() {
        if (numberOfEntries > (int) (0.3 * nButckets)) {
            int newBucketsNumber = nButckets * 2;
            String[] newKeyArray = new String[newBucketsNumber];
            String[] newValueArray = new String[newBucketsNumber];

            for (int i = 0; i < keyArray.length; i++) {
                if (keyArray[i] != null) {
                    int index = keyArray[i].hashCode() % newBucketsNumber;
                    newKeyArray[index] = keyArray[i];
                    newValueArray[index] = valueArray[keyArray[i].hashCode() % nButckets];
                }
            }           
            /*
            for (String key: this) {
                int index = key.hashCode() % newBucketsNumber;
                newKeyArray[index] = key;
                if (key == null) System.out.println(key); 
                newValueArray[index] = valueArray[key.hashCode() % nButckets];              
            }
            */
            keyArray = newKeyArray;
            valueArray = newValueArray;
            nButckets = newBucketsNumber;
        }
    }

    public void put(String key, String value) {
        int index = key.hashCode() % nButckets;
        int hasKey = hasKey(index, key);
        if (hasKey == 1) {
            valueArray[index] = value;
        } else if (hasKey == 0){
            keyArray[index] = key;
            valueArray[index] = value;
            numberOfEntries++;
        } else {
            rehashing();
            index = key.hashCode() % nButckets;
            keyArray[index] = key;
            valueArray[index] = value;
            numberOfEntries++;
        }
    }

    public String get(String key) {
        int index = key.hashCode() % nButckets;
        if (hasKey(index, key) == 1) return valueArray[index];
        return null;
    }

    private int hasKey(int index, String key) {
        if (keyArray[index] == null) {
            return 0;
        } else if (keyArray[index].equals(key)) {
            return 1;
        } else {
            return -1;
        }
    }

    public Iterator<String> iterator(){
        Iterator<String> iter = new Iterator<String>() {
            private int currentIndex = 0;
            private int nEntries = 0;

            @Override
            public boolean hasNext() {
                return nEntries < numberOfEntries && numberOfEntries != 0;
            }

            @Override
            public String next() {
                for (int i = currentIndex; i < keyArray.length; i++) {
                    if (keyArray[i] != null) {
                        currentIndex = i + 1;
                        nEntries++;
                        return keyArray[i];
                    }
                }
                return null;
            }
        };
        return iter;
    }

}

1 个答案:

答案 0 :(得分:0)

您的算法中int index = key.hashCode() % nButckets;1000的{​​{1}}值相同,即6357。因此,您的算法会使用1423

覆盖keyArray[1423]=1000

当您打印keyArray[1423]=6357时,对m.get(String.valueOf(1000))以及get()进行index方法检查后,它将返回key。阅读代码中的注释以获得进一步说明。

null