Guest User

java hashmap

a guest
Jun 23rd, 2020
3,704
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1.  
  2. public class MyHashMap<K, V> {
  3.  
  4. private class Entry<K, V> {
  5. private K key;
  6. private V value;
  7. private Entry<K, V> next;
  8.  
  9. public Entry(K key, V value) {
  10. this.key = key;
  11. this.value = value;
  12. }
  13.  
  14. public K getKey() {
  15. return this.key;
  16. }
  17.  
  18. public V getValue() {
  19. return this.value;
  20. }
  21.  
  22. public void setValue(V value) {
  23. this.value = value;
  24. }
  25.  
  26. @Override
  27. public String toString() {
  28. Entry<K,V> temp = this;
  29. StringBuilder sb = new StringBuilder();
  30. while (temp != null) {
  31. sb.append(temp.key + " -> " + temp.value + ",");
  32. temp = temp.next;
  33. }
  34. return sb.toString();
  35. }
  36.  
  37. }
  38.  
  39. private final int SIZE = 5;
  40.  
  41. private Entry<K, V> table[];
  42.  
  43. public MyHashMap() {
  44. table = new Entry[SIZE];
  45. }
  46.  
  47. public void put(K key, V value) {
  48. int hash = key.hashCode() % SIZE;
  49. Entry<K, V> e = table[hash];
  50.  
  51. if (e == null) {
  52. table[hash]= new Entry<K, V>(key, value);
  53. } else {
  54. while (e.next != null) {
  55. if (e.getKey() == key) {
  56. e.setValue(value);
  57. return;
  58. }
  59. e = e.next;
  60. }
  61.  
  62. if (e.getKey() == key) {
  63. e.setValue(value);
  64. return;
  65. }
  66.  
  67. e.next = new Entry<K, V>(key, value);
  68. }
  69. }
  70.  
  71. public V get(K key) {
  72. int hash = key.hashCode() % SIZE;
  73. Entry<K, V> e = table[hash];
  74.  
  75. if (e == null) {
  76. return null;
  77. }
  78.  
  79. while (e != null) {
  80. if (e.getKey() == key) {
  81. return e.getValue();
  82. }
  83. e = e.next;
  84. }
  85.  
  86. return null;
  87. }
  88.  
  89. public Entry<K, V> remove(K key) {
  90. int hash = key.hashCode() % SIZE;
  91. Entry<K, V> e = table[hash];
  92.  
  93. if (e == null) {
  94. return null;
  95. }
  96.  
  97. if (e.getKey() == key) {
  98. table[hash] = e.next;
  99. e.next = null;
  100. return e;
  101. }
  102.  
  103. Entry<K, V> prev = e;
  104. e = e.next;
  105.  
  106. while (e != null) {
  107. if (e.getKey() == key) {
  108. prev.next = e.next;
  109. e.next = null;
  110. return e;
  111. }
  112. prev = e;
  113. e = e.next;
  114. }
  115.  
  116. return null;
  117. }
  118.  
  119. @Override
  120. public String toString() {
  121. StringBuilder sb = new StringBuilder();
  122. for (int i = 0; i < SIZE; i++) {
  123. if (table[i] != null) {
  124. sb.append(i + " " + table[i] + "\n");
  125. } else {
  126. sb.append(i + " " + "null" + "\n");
  127. }
  128. }
  129.  
  130. return sb.toString();
  131. }
  132.  
  133. }
  134.  
  135.  
  136.  
  137. public class Driver {
  138.  
  139. public static void main(String[] args) {
  140. // TODO Auto-generated method stub
  141. MyHashMap<String, String> myHashMap = new MyHashMap<String, String>();
  142. myHashMap.put("A", "B");
  143. myHashMap.put("E", "F");
  144. myHashMap.put("H", "P");
  145. myHashMap.put("P", "2");
  146. myHashMap.put("1", "G");
  147. myHashMap.put("2", "6");
  148. myHashMap.put("3", "2");
  149. myHashMap.put("4", "4");
  150. myHashMap.put("1", "H");
  151.  
  152.  
  153. // System.out.println(myHashMap);
  154. // System.out.println(myHashMap.get("A"));
  155. // System.out.println(myHashMap.remove("A"));
  156. myHashMap.remove("2");
  157. System.out.println(myHashMap);
  158. }
  159.  
  160. }
Advertisement
Add Comment
Please, Sign In to add comment