Advertisement
Guest User

Untitled

a guest
Apr 29th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.62 KB | None | 0 0
  1. // Data Structures: Abstraction and Design Using Java, Koffman, Wolfgang
  2.  
  3. package KW.CH07;
  4.  
  5. import java.util.AbstractMap;
  6. import java.util.Iterator;
  7. import java.util.LinkedList;
  8. import java.util.List;
  9. import java.util.Map;
  10. import java.util.StringJoiner;
  11.  
  12. /**
  13. * Hash table implementation using chaining.
  14. * @param <K> The key type
  15. * @param <V> The value type
  16. * @author Koffman and Wolfgang
  17. **/
  18. public class HashtableChain<K, V>
  19. // Insert solution to programming project 7, chapter -1 here
  20. implements KWHashMap<K, V> {
  21.  
  22. /** The table */
  23. private LinkedList<Entry<K, V>>[] table;
  24. /** The number of keys */
  25. private int numKeys;
  26. /** The capacity */
  27. private static final int CAPACITY = 101;
  28. /** The maximum load factor */
  29. private static final double LOAD_THRESHOLD = 3.0;
  30.  
  31. // Note this is equivalent to java.util.AbstractMap.SimpleEntry
  32. /** Contains key-value pairs for a hash table.
  33. @param <K> the key type
  34. @param <V> the value type
  35. */
  36. public static class Entry<K, V>
  37. // Insert solution to programming project 6, chapter -1 here
  38. {
  39.  
  40. /** The key */
  41. private final K key;
  42. /** The value */
  43. private V value;
  44.  
  45. /**
  46. * Creates a new key-value pair.
  47. * @param key The key
  48. * @param value The value
  49. */
  50. public Entry(K key, V value) {
  51. this.key = key;
  52. this.value = value;
  53. }
  54.  
  55. /**
  56. * Retrieves the key.
  57. * @return The key
  58. */
  59. @Override
  60. public K getKey() {
  61. return key;
  62. }
  63.  
  64. /**
  65. * Retrieves the value.
  66. * @return The value
  67. */
  68. @Override
  69. public V getValue() {
  70. return value;
  71. }
  72.  
  73. /**
  74. * Sets the value.
  75. * @param val The new value
  76. * @return The old value
  77. */
  78. @Override
  79. public V setValue(V val) {
  80. V oldVal = value;
  81. value = val;
  82. return oldVal;
  83. }
  84.  
  85. // Insert solution to programming exercise 3, section 4, chapter 7 here
  86. }
  87.  
  88. // Constructor
  89. public HashtableChain() {
  90. table = new LinkedList[CAPACITY];
  91. }
  92.  
  93. // Constructor for test purposes
  94. HashtableChain(int capacity) {
  95. table = new LinkedList[capacity];
  96. }
  97.  
  98. /**
  99. * Method get for class HashtableChain.
  100. * @param key The key being sought
  101. * @return The value associated with this key if found;
  102. * otherwise, null
  103. */
  104. @Override
  105. public V get(Object key) {
  106. int index = key.hashCode() % table.length;
  107. if (index < 0) {
  108. index += table.length;
  109. }
  110. if (table[index] == null) {
  111. return null; // key is not in the table.
  112. }
  113. // Search the list at table[index] to find the key.
  114. for (Entry<K, V> nextItem : table[index]) {
  115. if (nextItem.getKey().equals(key)) {
  116. return nextItem.getValue();
  117. }
  118. }
  119.  
  120. // assert: key is not in the table.
  121. return null;
  122. }
  123.  
  124. /**
  125. * Method put for class HashtableChain.
  126. * @post This key-value pair is inserted in the
  127. * table and numKeys is incremented. If the key is already
  128. * in the table, its value is changed to the argument
  129. * value and numKeys is not changed.
  130. * @param key The key of item being inserted
  131. * @param value The value for this key
  132. * @return The old value associated with this key if
  133. * found; otherwise, null
  134. */
  135. @Override
  136. public V put(K key, V value) {
  137. int index = key.hashCode() % table.length;
  138. if (index < 0) {
  139. index += table.length;
  140. }
  141. if (table[index] == null) {
  142. // Create a new linked list at table[index].
  143. table[index] = new LinkedList<>();
  144. }
  145.  
  146. // Search the list at table[index] to find the key.
  147. for (Entry<K, V> nextItem : table[index]) {
  148. // If the search is successful, replace the old value.
  149. if (nextItem.getKey().equals(key)) {
  150. // Replace value for this key.
  151. V oldVal = nextItem.getValue();
  152. nextItem.setValue(value);
  153. return oldVal;
  154. }
  155. }
  156.  
  157. // assert: key is not in the table, add new item.
  158. table[index].addFirst(new Entry<>(key, value));
  159. numKeys++;
  160. if (numKeys > (LOAD_THRESHOLD * table.length)) {
  161. rehash();
  162. }
  163. return null;
  164. }
  165.  
  166. /** Returns true if empty
  167. @return true if empty
  168. */
  169. @Override
  170. public boolean isEmpty() {
  171. return numKeys == 0;
  172. }
  173.  
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement