HTMap.java 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import org.w3c.dom.Node;
  2. public class HTMap<K, V> {
  3. public static void main(String[] args) {
  4. HTMap<String, Integer> htmap = new HTMap<>();
  5. htmap.put("radiohead", 10);
  6. htmap.put("kanye", 9);
  7. htmap.put("t swizzle", 3);
  8. htmap.put("black sabbath", 8);
  9. System.out.println(htmap.containsKey("black sabbath"));
  10. System.out.println(htmap.containsValue(10));
  11. System.out.println(htmap.toString());
  12. }
  13. private class Entry<K, V> {
  14. K key;
  15. V value;
  16. @Override
  17. public int hashCode() {
  18. return key.hashCode();
  19. }
  20. }
  21. private Entry<K, V>[] table;
  22. private int size;
  23. public HTMap() {
  24. table = (Entry<K, V>[]) new Entry[10];
  25. }
  26. public V put(K key, V value) {
  27. int i = Math.abs(key.hashCode()) % table.length;
  28. while (table[i] != null && !table[i].key.equals(key))
  29. i = (i + 1) % table.length;
  30. if (table[i] == null) {
  31. Entry<K, V> entry = new Entry<>();
  32. entry.key = key;
  33. entry.value = value;
  34. table[i] = entry;
  35. size++;
  36. if (((double) size) / table.length > 0.75)
  37. rehash();
  38. return null;
  39. } else {
  40. V old = table[i].value;
  41. table[i].value = value;
  42. return old;
  43. }
  44. }
  45. public V get(K key) {
  46. int i = Math.abs(key.hashCode()) % table.length;
  47. while (table[i] != null && !table[i].key.equals(key))
  48. i = (i + 1) % table.length;
  49. if (table[i] == null)
  50. return null;
  51. else
  52. return table[i].value;
  53. }
  54. public int size() {
  55. return size;
  56. }
  57. private void rehash() {
  58. Entry<K, V>[] small = table;
  59. table = (Entry<K, V>[]) new Entry[size + size];
  60. size = 0;
  61. for (int i = 0; i < small.length; i++)
  62. if (small[i] != null)
  63. put(small[i].key, small[i].value);
  64. System.out.println("Rehash! (" + size + ")");
  65. }
  66. // Returns true if the map contains the given key.
  67. public boolean containsKey(K key) {
  68. return get(key) != null ? true : false;
  69. }
  70. // Returns true if the map contains the given value.
  71. public boolean containsValue(V value) {
  72. for (int i = 0; i < table.length; i++) {
  73. if (table[i] != null && table[i].value == value)
  74. return true;
  75. }
  76. return false;
  77. }
  78. // Returns the map formatted as a string (details below).
  79. @Override
  80. public String toString() {
  81. if (size() == 0)
  82. return "[]";
  83. int found = 0;
  84. String s = "[";
  85. for (int i = 0; i < table.length; i++) {
  86. if (table[i] == null)
  87. continue;
  88. found++;
  89. if (found < size()) {
  90. s += "{" + table[i].key + "|" + table[i].value + "}, ";
  91. } else {
  92. s += "{" + table[i].key + "|" + table[i].value + "}]";
  93. }
  94. }
  95. return s;
  96. }
  97. }