如何在哈希映射中找到平均桶长度?

时间:2016-10-10 06:15:37

标签: java hashmap

我正在做一个类赋值,它要求我们创建一个HashMap,然后用随机值填充它,首先没有动态调整大小,然后使用动态调整大小,以便查看较小的桶大小在存储值时所产生的速度差异。

为了做到这一点,它要求我创建一个方法,在HashMap中找到最短的桶长度,平均桶长度和最长的桶长度。

这是我遇到问题的地方,因为当我尝试获取桶的长度时,它总是返回零。我不确定我的问题在哪里,因为我的代码逻辑上对我有意义。任何帮助将不胜感激。

下面是我的HashMap和调用它的主程序的代码。

public class MyHashMap<K, V> {
    private LinkedList<KVP <K, V>>[] bucket;
    private int M = 5;
    private int size = 0;


    public MyHashMap(int M){
        this.bucket = (LinkedList<KVP<K, V>>[]) new LinkedList[M];
        for(int i = 0; i < M; i++){
            bucket[i] = new LinkedList<KVP<K, V>>();
        }
    }
    public MyHashMap(){
        this.bucket = (LinkedList<KVP<K, V>>[]) new LinkedList[M];
        for(int i = 0; i < 5; i++){
            bucket[i] = new LinkedList<KVP<K, V>>();
        }
    }

    public int getBucketNumber(K key){
        return Math.abs(key.hashCode()) % this.bucket.length;
    }
    public void put(K key, V value){
        //resize();
        size++;
        int b = getBucketNumber(key);
        for(KVP<K, V> pair : this.bucket[b]){
            if(pair.key.equals(key)){
                pair.value = value;
                return;
            }
        }
        KVP<K, V> pair = new KVP<>(key, value);
        this.bucket[b].add(pair);
    }
    public int size(){
        return this.bucket.length;
    }
    public boolean isEmpty(){
        return size == 0;
    }
    public void resize(){
      if(size > size() * 2){
          LinkedList<KVP <K, V>>[] newHash = (LinkedList<KVP<K, V>>[]) new LinkedList[2 * size()];
          for(int i = 0; i < newHash.length; i++){
              newHash[i] = new LinkedList<KVP <K, V>>();
          }
          for(LinkedList<KVP<K, V>> map: this.bucket){
              for(KVP<K, V> pair: map){
                  int newHashCode = Math.abs(pair.key.hashCode()) % newHash.length;
                  newHash[newHashCode].add(pair);
              }
          }
          this.bucket = newHash;
      }     
    }

    public void delete(K key){
        int b = getBucketNumber(key);
        for(KVP<K, V> pair : this.bucket[b]){
            if(pair.key.equals(key)){
                key = null;
            }
        }
    }
    public boolean containsKey(K key){
        int b = getBucketNumber(key);
        boolean t = false;
        for(KVP<K, V> pair : this.bucket[b]){
            if(pair.key.equals(key)){
                t = true;
            }else{
                t = false;
            }
        }
            return t;
    }
    public V get(K key){
        int b = getBucketNumber(key);
        for(KVP<K, V> pair : this.bucket[b]){
            if(pair.key.equals(key)){
                return pair.value;
            }
        }
        return null;
    }
    public int Avg(){
        int j = 0;
        for(int i = 0; i < bucket.length; i++){
            j += bucket[i].size();
        }
        return j/bucket.length;
    }

}

public class hwMain {
    public static void main(String[] args){

        int N = (int) Math.pow(10, 3);
        MyHashMap<Integer, Integer> store = new MyHashMap<>();      
        for(int i = 0; i < N; i++){

            store.put((int) Math.random(), 1);
            }
        System.out.print(store.Avg());
    }
}

1 个答案:

答案 0 :(得分:0)

因为根据Math.random()

  

返回带有正号的double值,大于或等于   0.0且小于1.0。

由于数字始终小于1.0,因此执行此操作时:

(int) Math.random()

这总是返回0(零)。

<强>解决方案:

如果您有一系列随机数和最小值,请使用:

(int) (Math.random() * range) + minimum

例如:您想要从1到100获取随机数。

(int) (Math.random() * 100) + 1