#Struggling with HashTables, need to write two complementary methods for a class

17 messages · Page 1 of 1 (latest)

proud granite
#

Hello I have a pre written class that I need to write two methods for.

cinder roostBOT
#

This post has been reserved for your question.

Hey @proud granite! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

proud granite
#
   private int size;
   private Node<K, V>[] table;
    
   public SimpleHashMap(int capacity) {
      table = (Node<K, V>[]) new Node[capacity];
   }
    
   public SimpleHashMap() {
      this(16);
   }
    
   private int index(K key) {
        // Finished
   }

   private Node<k, V> findNode(int index, K key) {
       // Finished
   }
   
   // Övriga metoder i klassen put, get ...
    
   private static class Node<K, V> {
      private K key;
      private V value;
      private Node<k, V> next;

      private Node(K key, V value) {
         this.key = key;
         this.value = value;
         this.next = null;
      }
   }
}```
#

This is the class I was given

#

And I need to write these methods, but not sure where to start. ```/**

  • Associates the specified value with the specified key in this map. If the map
  • previously contained a mapping for the key, the old value is replaced.
    */
    public V put(K key, V value) {

}

/**

  • Returns the value to which the specified key is mapped, or null if this map
  • contains no mapping for the key.
    */
    public V get(K key) {
    // Fyll i egen kod här
    }```
rare pebble
#

Do you know how hash tables work?

#

and how to set values in hash tables

proud granite
#

It is what the exercise is for

#

But I do know how they work basically. In theory that is

#

It is a vector with indexes, where each index is kind of another vector with values,

#

and the places where these indexes lie, are the keys

#

If I remember correctly

rare pebble
#

Do you know how to find the position of an element in the hash table?

proud granite
cinder roostBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.