title: How to find the average bucket length in a hash map?
body: <p>I am doing a class assignment and it asks us to create a HashMap and then fill it with random values, first without the dynamic resizing and then with the dynamic resizing in order to see the speed difference smaller bucket sizes makes in storing values. </p>
  
  <p>In order to do this it asks me to create a method that finds the shortest bucket length, the average bucket length, and the longest bucket length in the HashMap. </p>
  
  <p>This is where I am running into problems as when I try and get the lengths of the buckets it always returns zero. I'm not sure where my problem lies as my code logically makes sense to me. Any help would be greatly appreciated. </p>
  
  <p>Below is the code for my HashMap and my Main program that is calling it.</p>
  
  <pre><code>public class MyHashMap&lt;K, V&gt; {
      private LinkedList&lt;KVP &lt;K, V&gt;&gt;[] bucket;
      private int M = 5;
      private int size = 0;
  
  
      public MyHashMap(int M){
          this.bucket = (LinkedList&lt;KVP&lt;K, V&gt;&gt;[]) new LinkedList[M];
          for(int i = 0; i &lt; M; i++){
              bucket[i] = new LinkedList&lt;KVP&lt;K, V&gt;&gt;();
          }
      }
      public MyHashMap(){
          this.bucket = (LinkedList&lt;KVP&lt;K, V&gt;&gt;[]) new LinkedList[M];
          for(int i = 0; i &lt; 5; i++){
              bucket[i] = new LinkedList&lt;KVP&lt;K, V&gt;&gt;();
          }
      }
  
      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&lt;K, V&gt; pair : this.bucket[b]){
              if(pair.key.equals(key)){
                  pair.value = value;
                  return;
              }
          }
          KVP&lt;K, V&gt; pair = new KVP&lt;&gt;(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 &gt; size() * 2){
            LinkedList&lt;KVP &lt;K, V&gt;&gt;[] newHash = (LinkedList&lt;KVP&lt;K, V&gt;&gt;[]) new LinkedList[2 * size()];
            for(int i = 0; i &lt; newHash.length; i++){
                newHash[i] = new LinkedList&lt;KVP &lt;K, V&gt;&gt;();
            }
            for(LinkedList&lt;KVP&lt;K, V&gt;&gt; map: this.bucket){
                for(KVP&lt;K, V&gt; 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&lt;K, V&gt; 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&lt;K, V&gt; 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&lt;K, V&gt; 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 &lt; 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&lt;Integer, Integer&gt; store = new MyHashMap&lt;&gt;();      
          for(int i = 0; i &lt; N; i++){
  
              store.put((int) Math.random(), 1);
              }
          System.out.print(store.Avg());
      }
  }
  </code></pre>