Hashtable.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. public class Hashtable<T> {
  2. public static void main(String[] args) {
  3. Hashtable<String> ht = new Hashtable<String>();
  4. String s1 =
  5. "The divine rights of kings was a political " +
  6. "philosophy espoused by Chucky One back in the " +
  7. "day who lost his head on account of him being " +
  8. "a dumbass";
  9. String[] words = s1.split(" ");
  10. for (String s : words) ht.add(s);
  11. ht.printArray();
  12. int longestChain = ht.longestChain();
  13. int numberOfChains = ht.numberOfChains();
  14. // LINEAR PROBING :
  15. // Longest Chain : 4
  16. // Number of Chains : 3
  17. //
  18. // QUADRADIC PROBING :
  19. // Longest Chain : 4
  20. // Number of Chains : 2
  21. //
  22. // Less chains = quicker fetching
  23. System.out.println(
  24. "Longest Chain : " +
  25. longestChain +
  26. "\nNumber of Chains : " +
  27. numberOfChains
  28. );
  29. }
  30. private T[] table;
  31. private int size;
  32. private float loadFactor;
  33. public Hashtable() {
  34. this(11);
  35. }
  36. public Hashtable(int initialCapacity) {
  37. table = (T[]) new Object[initialCapacity];
  38. size = 0;
  39. loadFactor = 0.75f;
  40. }
  41. public void add(T elem) {
  42. if (size / (double) table.length > loadFactor) rehash();
  43. int i = Math.abs(elem.hashCode()) % table.length;
  44. int t = 0;
  45. while (table[i] != null) {
  46. // Linear Probing
  47. // i = (i + 1) % table.length;
  48. // Quadratic Probing
  49. i = (i + (t ^ 2)) % table.length;
  50. t++;
  51. }
  52. table[i] = elem;
  53. size++;
  54. }
  55. public boolean contains(T elem) {
  56. int i = Math.abs(elem.hashCode()) % table.length;
  57. int t = 0;
  58. while (table[i] != null && !table[i].equals(elem)) {
  59. // Linear Probing
  60. // i = (i + 1) % table.length;
  61. // Quadratic
  62. i = (i + (t ^ 2)) % table.length;
  63. }
  64. return table[i] != null;
  65. }
  66. public void printArray() {
  67. for (int i = 0; i < table.length; i++) System.out.println(
  68. i + ": " + table[i]
  69. );
  70. }
  71. public int longestChain() {
  72. int longest = 0;
  73. for (int i = 0; i < table.length; i++) {
  74. int j = i;
  75. while (i < table.length && table[i] != null) {
  76. i++;
  77. }
  78. int lengthOfChain = i - j;
  79. if (lengthOfChain > longest) longest = lengthOfChain;
  80. }
  81. return longest;
  82. }
  83. public int numberOfChains() {
  84. int threeOrMoreChains = 0;
  85. for (int i = 0; i < table.length; i++) {
  86. int j = i;
  87. while (i < table.length && table[i] != null) {
  88. i++;
  89. }
  90. int lengthOfChain = i - j;
  91. if (lengthOfChain > 3) threeOrMoreChains++;
  92. }
  93. return threeOrMoreChains;
  94. }
  95. public int size() {
  96. return size;
  97. }
  98. private void rehash() {
  99. T[] smallTable = table;
  100. table = (T[]) new Object[table.length * 2 + 1];
  101. for (int i = 0; i < smallTable.length; i++) if (
  102. smallTable[i] != null
  103. ) add(smallTable[i]);
  104. }
  105. }