Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.81 KB | None | 0 0
  1. public boolean checkAndMove(Entry<K, V> entry) {
  2.   // calculate hashValue for key
  3.   int hashValue = hashValue(entry.getKey());
  4.  
  5.   // index of this entry is equal to hash value of key then return true
  6.   if (bucket[hashValue] == entry)
  7.    return true;
  8.  
  9.   // save old entry stored in index[hashValue]
  10.   Entry<K, V> entryToMove = bucket[hashValue];
  11.  
  12.   // this will be equal to index in bucket of entry in input
  13.   int index = hashValue;
  14.   // search entry in bucket
  15.   while (bucket[index] != null || bucket[index] == AVAILABLE) {
  16.    // linear probing
  17.    index = (index + 1) % capacity;
  18.    // this is to avoid an infinite loop or an IndexOutBoundException
  19.    if (index >= bucket.length)
  20.     index = 0;
  21.   }
  22.  
  23.   // swap elements
  24.   bucket[hashValue] = entry;
  25.   bucket[index] = entryToMove;
  26.  
  27.   return false;
  28.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement