Questions on implementing my own HashMap in Java

I am working on an assignment where I need to implement my own HashMap. In the destination text, it is described as an array of lists, and whenever you want to add an element, the place it falls into the array is determined by its hashCode. In my case, these are the positions from the spreadsheet, so I just took columnNumber + rowNumber and then converted it to String and then to int, like hashCode, and then paste this place into an array. Of course, it is inserted in the form of a Node (key, value), where the key is the position of the cell, and the value is the value of the cell.

But I have to say that I do not understand why we need an array of lists, because if in the end we get a list of more than one element, will it not significantly increase the search time? So shouldn't this be an array of nodes?

I also found this implementation of HashMap in Java:

public class HashEntry {
      private int key;
      private int value;

      HashEntry(int key, int value) {
            this.key = key;
            this.value = value;
      }     

      public int getKey() {
            return key;
      }

      public int getValue() {
            return value;
      }
}

public class HashMap {
  private final static int TABLE_SIZE = 128;

  HashEntry[] table;

  HashMap() {
        table = new HashEntry[TABLE_SIZE];
        for (int i = 0; i < TABLE_SIZE; i++)
              table[i] = null;
  }

  public int get(int key) {
        int hash = (key % TABLE_SIZE);
        while (table[hash] != null && table[hash].getKey() != key)
              hash = (hash + 1) % TABLE_SIZE;
        if (table[hash] == null)
              return -1;
        else
              return table[hash].getValue();
  }

  public void put(int key, int value) {
        int hash = (key % TABLE_SIZE);
        while (table[hash] != null && table[hash].getKey() != key)
              hash = (hash + 1) % TABLE_SIZE;
        table[hash] = new HashEntry(key, value);
  }
}

So, does the put method look right first in the [hash] table, and if it is not empty, and if it does not have a key that is entered in the put method, then it moves to the table [(hash + 1)% TABLE_SIZE]. But if it is the same key, it just overwrites the value. So is this understood correctly? And because the get and put method uses the same method for finding a place in an array, which, given the same key, can end up in the same place in the array?

, , , , !

, HashMap Node, a Node , getHashCode, .

SinglyLinkedList ( ), .

Hash - hashCode% hashMap.length.

, ?

package spreadsheet; 

public class HashTableMap {

  private SinglyLinkedListMap[] hashArray;
  private int size;


  public HashTableMap() {
    hashArray = new SinglyLinkedListMap[64];
    size = 0;  
  }


  public void insert(final Position key, final Expression value) {

      Node node = new Node(key, value); 
      int hashNumber = node.getHashCode() % hashArray.length;       
      SinglyLinkedListMap bucket = new SinglyLinkedListMap();
      bucket.insert(key, value);
      if(hashArray[hashNumber] == null) {
        hashArray[hashNumber] = bucket;
        size++; 
      }
      if(hashArray[hashNumber] != null) {
        SinglyLinkedListMap bucket2 = hashArray[hashNumber];
        bucket2.insert(key, value);
        hashArray[hashNumber] = bucket2;
        size++; 
      }
      if (hashArray.length == size) {
          SinglyLinkedListMap[] newhashArray = new SinglyLinkedListMap[size * 2];
      for (int i = 0; i < size; i++) {
          newhashArray[i] = hashArray[i];
      }
      hashArray = newhashArray;
    }
  } 

  public Expression lookUp(final Position key) {
      Node node = new Node(key, null); 
      int hashNumber = node.getHashCode() % hashArray.length;
      SinglyLinkedListMap foundBucket = hashArray[hashNumber];
      return foundBucket.lookUp(key); 
  }
 }

O (1), , ? , , ?

+5
4

- -, , .

.

, , , , . , .

+9

, hashcode. , : hashcode return 1. , , - . , .

, . , - , , .

( ) , hashcode, , . , . . - "", -. , .

, hashcode " " , 1 .

+1

Lists . - mod TABLE SIZE, , .

, - , key - -, . , (2,1) (1,2) 3, , . , (12,1) (1, 21) --- "121". (, ) .

, hashcodes - TABLE_SIZE. .

0
class SpreadSheetPosition {
    int column;
    int row;

    @Override
    public int hashCode() {
        return column + row;
    }
}

class HashMap {
    private Liat[] buckets = new List[N];

    public void put(Object key, Object value) {
        int keyHashCode = key.hashCode();
        int bucketIndex = keyHashCode % N;
        ...
    }
}

Compare, having N lists, having only one list / array. To search the list you need to go through, perhaps the entire list. Using an array of lists at least reduces individual lists. It is even possible to get a list of one or null element (null).

If hashCode()as unique as possible, the chance of immediate detection is high.

0
source

All Articles