0

I created a HashMap to store entries. Now when there is a collision of hashcodes, how can I resize the buckets. Right now I just have the buckets set to an arbitrary prime number and I can't figure out how to make the buckets resize themselves if buckets reach capacity.

public class BucketList<K, V> extends LinkedList<Bucket<K, V>> {

    private final int NUM_BUCKETS = 997; //Arbitrary prime number 

    public BucketList() {
        initializeBuckets();
    }
    public void add(int index, BucketEntry<K, V> entry) {

        Bucket<K, V> bucket = get(index);

        if (bucket == null){
            set(index, new Bucket<>(entry));
        } else {
            BucketEntry<K, V> current = bucket.findEntry(entry.getKey());

            if (current != null) {
                current.setValue(entry.getValue());
            } else
            {
                bucket.add(entry);
            }
        }
    }

    private void initializeBuckets() {
        for (int i = 0; i < NUM_BUCKETS; i++) {
            add(null);
        }
    }
}
Kalamarico
  • 5,466
  • 22
  • 53
  • 70
  • Please be more specific with where you are stuck. – Joe C Sep 30 '17 at 19:20
  • You can try to implement the Chaining Method for avoiding collision, but I don't suggest you to do this. It's better if you will use some collections like HashMap or HashSet, because these two already have an algorithm for collision avoiding. This might help you: https://stackoverflow.com/questions/4980757/how-do-hashtables-deal-with-collisions – Dina Bogdan Sep 30 '17 at 19:29
  • as you can see in intializeBuckets(), I make 997 buckets, but id like to make it so it resizes to the amount of "stuff" that comes in. – Ryan Higgs Oct 02 '17 at 03:09

0 Answers0