Advertisement
Guest User

Untitled

a guest
Nov 13th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. package cs2321;
  2.  
  3. import net.datastructures.Entry;
  4. import net.datastructures.Map;
  5.  
  6. /*
  7. * @author Timothy Laynes
  8. * CS2321
  9. */
  10. public class HashMap<K, V, E> extends AbstractMap<K, V> implements Map<K, V> {
  11.  
  12. /*
  13. * Use Array of UnorderedMap<K,V> for the Underlying storage for the map of
  14. * entries.
  15. *
  16. */
  17.  
  18. private UnorderedMap<K, V>[] table;
  19. int size; // number of mappings(entries)
  20. int capacity; // The size of the hash table.
  21.  
  22. /**
  23. * Constructor that takes a hash size
  24. *
  25. * @param hashtable size: the number of buckets to initialize
  26. */
  27. public HashMap(int hashtablesize) {
  28. capacity = hashtablesize;
  29. table = (UnorderedMap<K, V>[]) new UnorderedMap[capacity];
  30.  
  31. }
  32.  
  33. /**
  34. * Constructor that takes no argument Initialize the hash table with default
  35. * hash table size: 17
  36. */
  37. public HashMap() {
  38. this(17);
  39.  
  40. }
  41.  
  42. /*
  43. * This method should be called by map an integer to the index range of the hash
  44. * table
  45. */
  46. private int hashValue(K key) {
  47. return Math.abs(key.hashCode()) % capacity;
  48. }
  49.  
  50. /*
  51. * The purpose of this method is for testing if the table was doubled when
  52. * rehashing is needed. Return the the size of the hash table. It should be 17
  53. * initially, after the load factor is more than 0.75, it should be doubled.
  54. */
  55.  
  56. @Override
  57. public int size() {// returns the size of the Hash map
  58. return size;
  59. }
  60.  
  61. @Override
  62. public boolean isEmpty() {// returns a boolean indicating if its empty
  63. // TODO Auto-generated method stub
  64. return size() == 0;
  65. }
  66.  
  67. @TimeComplexity("0(1)")
  68. @Override
  69. /*
  70. *
  71. */
  72. public V get(K key) {
  73. UnorderedMap<K, V> map = table[hashValue(key)];
  74. if (map == null)
  75. return null;
  76. return map.get(key);
  77. }
  78.  
  79. @TimeComplexity("0(1)")
  80. @Override
  81. public V put(K key, V value) {
  82. UnorderedMap<K, V> map = table[hashValue(key)];
  83. V answer = null;
  84. if (map == null) {
  85. map = table[hashValue(key)] = new UnorderedMap<>();
  86.  
  87. int oldSize = map.size();
  88. answer = map.put(key, value);
  89. size += (map.size() - oldSize);
  90. }
  91.  
  92. return answer;
  93. }
  94.  
  95. @TimeComplexity("0(1)")
  96. @Override
  97. public V remove(K key) {
  98. UnorderedMap<K, V> map = table[hashValue(key)];
  99. if (map == null) {
  100. return null;
  101. }
  102. int oldSize = map.size();
  103. V answer = map.remove(key);
  104. size -= (oldSize - map.size());
  105.  
  106. return answer;
  107. }
  108.  
  109. @Override
  110. public Iterable<Entry<K, V>> entrySet() {
  111. ArrayList<Entry<K, V>> buffer = new ArrayList<>();
  112. for (int h = 0; h < capacity; h++) {
  113. if (table[h] != null) {
  114. for (Entry<K, V> entry : table[h].entrySet()) {
  115. buffer.addLast(entry);
  116. }
  117.  
  118. }
  119. }
  120. return buffer;
  121.  
  122. }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement