Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. package map;
  2.  
  3. public class SimpleHashMap<K, V> implements Map<K, V> {
  4. private Entry<K, V>[] table;
  5. private double loadFactor;
  6. private int size;
  7.  
  8. public SimpleHashMap() {
  9. table = (Entry<K, V>[]) new Entry[16];
  10. loadFactor = 0.75;
  11. size = 0;
  12. }
  13.  
  14. public SimpleHashMap(int capacity) {
  15. table = (Entry<K, V>[]) new Entry[capacity];
  16. loadFactor = 0.75;
  17. }
  18.  
  19. public String show() {
  20. StringBuilder sb = new StringBuilder();
  21. for (int i = 0; i < table.length; i++) {
  22. sb.append(i + "\t");
  23. Entry<K, V> temp = table[i];
  24. while (temp != null) {
  25. sb.append(temp.toString());
  26. // Behöver ej fungera
  27. temp = (Entry<K, V>) temp.next;
  28. }
  29. sb.append("\n");
  30. }
  31. return sb.toString();
  32. }
  33.  
  34. @Override
  35. public V get(Object arg0) {
  36. // TODO Auto-generated method stub
  37. return null;
  38. }
  39.  
  40. @Override
  41. public boolean isEmpty() {
  42. for (int i = 0; i < table.length; i++) {
  43. if (table[i] != null) {
  44. return false;
  45. }
  46. }
  47. return true;
  48. }
  49.  
  50. @Override
  51. public V put(K arg0, V arg1) {
  52.  
  53. size++;
  54. if((double)size/(double)table.length>0.75){
  55. rehash();
  56. }
  57. if(table[index(arg0)]==null){
  58. Entry<K,V> neu = new Entry(arg0,arg1);
  59. table[index(arg0)]= neu;
  60. return null;
  61. }
  62. Entry<K,V> temp = table[index(arg0)];
  63. while(temp!=null){
  64. if(temp.key.equals(arg0)){
  65. V tempv = temp.value;
  66. temp.value=arg1;
  67. return tempv;
  68. }else if (temp.next==null){
  69. temp = table[index(arg0)];
  70. table[index(arg0)]= new Entry(arg0,arg1);
  71. table[index(arg0)].next = (Entry<K, V>) temp;
  72. return null;
  73. }
  74. temp = (Entry<K, V>) temp.next;
  75. }
  76.  
  77. return null;
  78. }
  79.  
  80. private void rehash(){
  81. SimpleHashMap s = new SimpleHashMap(2*table.length);
  82. for (int i = 0; i < table.length; i++) {
  83. Entry<K,V> temp = table[i];
  84. while(temp!=null){
  85. s.put(temp.key, temp.value);
  86. temp = (Entry<K, V>) temp.next;
  87. }
  88. }
  89. table = s.table;
  90. }
  91.  
  92.  
  93.  
  94. @Override
  95. public V remove(Object arg0) {
  96. // TODO Auto-generated method stub
  97. return null;
  98. }
  99.  
  100. @Override
  101. public int size() {
  102. return size;
  103. }
  104.  
  105. private int index(K key) {
  106. int index = key.hashCode() % table.length;
  107.  
  108. // Math.abs används inte då det kan returnera negativa värden
  109. if (index < 0) {
  110. index = +table.length;
  111. }
  112. return index;
  113. }
  114.  
  115. private Entry<K, V> find(int index, K key) {
  116.  
  117. Entry<K, V> temp = table[index];
  118.  
  119. while (temp != null) {
  120. if (temp.key.equals(key)) {
  121. return temp;
  122. }
  123. // Behöver fungera
  124. temp = (Entry<K, V>) temp.next;
  125. }
  126. return null;
  127.  
  128. /*
  129. * while(temp!=null){ if(temp.equals(key)){ return temp; } temp =
  130. * (Entry<K, V>) temp.next; } return null;
  131. */
  132. }
  133.  
  134. private static class Entry<K, V> implements Map.Entry<K, V> {
  135. private K key;
  136. private V value;
  137. private Entry<K, V> next;
  138.  
  139. public Entry(K k, V v) {
  140. value = v;
  141. key = k;
  142. }
  143.  
  144. @Override
  145. public V get(Object arg0) {
  146. // TODO Auto-generated method stub
  147. return null;
  148. }
  149.  
  150. @Override
  151. public boolean isEmpty() {
  152. // TODO Auto-generated method stub
  153. return false;
  154. }
  155.  
  156. @Override
  157. public V put(K arg0, V arg1) {
  158. Entry<K, V> neu = new Entry(arg0, arg1);
  159. return null;
  160. }
  161.  
  162. @Override
  163. public V remove(Object arg0) {
  164. // TODO Auto-generated method stub
  165. return null;
  166. }
  167.  
  168. @Override
  169. public int size() {
  170. // TODO Auto-generated method stub
  171. return 0;
  172. }
  173.  
  174. public String toString() {
  175. return key + "=" + value;
  176. }
  177.  
  178. }
  179.  
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement