Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. class SolutionHashTable {
  2. public Entry[] table;
  3. public int capacity;
  4.  
  5. /**
  6. * Constructs a new HashTable.
  7. *
  8. * Capacity of the hash table can not be 0 or negative.
  9. *
  10. * @param capacity
  11. * to be used as capacity of the table.
  12. * @throws IllegalArgumentException
  13. * if the input capacity is invalid.
  14. */
  15. public SolutionHashTable(int capacity) {
  16. if(capacity <= 0)throw new IllegalArgumentException();
  17. table = new Entry[capacity];
  18. this.capacity = capacity;
  19. }
  20.  
  21. /**
  22. * Add a new Entry to the hash table,
  23. * uses linear probing to deal with collisions.
  24. *
  25. * Returns false, if the key is null or the table is full.
  26. *
  27. * @param key
  28. * String representing the key of the entry.
  29. * @param value
  30. * String representing the value of the entry.
  31. * @return true iff entry has been added successfully, else false.
  32. */
  33. public boolean put(String key, String value) {
  34.  
  35. if(key == null)return false;
  36. int hash = hash(key);
  37. Entry e = new Entry(key,value);
  38. for(int i = 0; i < capacity; i++){
  39. Entry en = table[hash];
  40. if(en == null || (en.getKey() == null && en.getValue() == null) || en.getKey() == e.getKey()){
  41. table[hash] = e;
  42. return true;
  43. }
  44. hash = (hash+1) % capacity;
  45. }
  46. return false;
  47. }
  48.  
  49. /**
  50. * Retrieve the value of the entry associated with this key.
  51. *
  52. * Returns null, if the key is null.
  53. *
  54. * @param key
  55. * String representing the key of the entry to look for.
  56. * @return value of the entry as String iff the entry with this key is found in the hash table, else null.
  57. */
  58. public String get(String key) {
  59. if(key == null)return null;
  60. int hash = hash(key);
  61.  
  62. for(int i = 0; i < capacity; i++){
  63. if(table[hash] == null)return null;
  64.  
  65. if(table[hash].getKey() == key){
  66. return table[hash].getValue();
  67. }
  68. hash = (hash+1) % capacity;
  69. }
  70. return null;
  71. }
  72.  
  73. /**
  74. * Remove the entry associated with this key from the hash table.
  75. *
  76. * Returns false, if the key is null.
  77. *
  78. * @param key
  79. * String representing the key of the entry to remove.
  80. * @return true iff the entry has been successfully removed, else false.
  81. */
  82. public boolean remove(String key) {
  83.  
  84. if(key == null)return false;
  85. int hash = hash(key);
  86.  
  87. for(int i = 0; i < capacity; i++){
  88. if(table[hash] == null)return false;
  89. if(table[hash].getKey() == key){
  90. setDefunct(hash);
  91. return true;
  92. }
  93. hash = (hash+1) % capacity;
  94. }
  95. return false;
  96. }
  97.  
  98. /**
  99. * Takes as input an index and sets the entry in that location as defunct.
  100. *
  101. * @param index
  102. * The index of the spot that is defunct.
  103. */
  104. public void setDefunct(int index) {
  105. this.table[index] = new Entry(null, null);
  106. }
  107.  
  108. /**
  109. * Hashes a string representing a key.
  110. *
  111. * @param key
  112. * String that needs to be hashed.
  113. * @return the hashcode of the string, modulo the capacity of the HashTable.
  114. */
  115. public int hash(String key) {
  116. return Math.abs(key.hashCode()) % capacity;
  117. }
  118. }
  119. //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement