Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.Scanner;
  4.  
  5. class HTChaining {
  6.  
  7. public static void main(String[] args) {
  8.  
  9. Scanner scan = new Scanner(System.in);
  10.  
  11. int tamanho = Integer.parseInt(scan.nextLine());
  12. TabelaHash<Integer, String> tabela = new TabelaHash<>(tamanho);
  13.  
  14. Integer key;
  15. String value;
  16. String entrada[];
  17. String operacao = "";
  18.  
  19. while (!operacao.equals("end")) {
  20. entrada = scan.nextLine().split(" ");
  21. operacao = entrada[0];
  22.  
  23. switch (operacao) {
  24. case "put":
  25. key = Integer.parseInt(entrada[1]);
  26. value = entrada[2];
  27. tabela.put(key, value);
  28. break;
  29.  
  30. case "remove":
  31. key = Integer.parseInt(entrada[1]);
  32. tabela.remove(key);
  33. break;
  34.  
  35. case "keys":
  36. System.out.println(Arrays.toString(tabela.keys()));
  37. break;
  38.  
  39. case "values":
  40. System.out.println(Arrays.toString(tabela.values()));
  41. break;
  42. }
  43.  
  44. }
  45. scan.close();
  46. }
  47. }
  48.  
  49. class TabelaHash<K, V> {
  50.  
  51. private ArrayList<Pair<K, V>>[] tabela;
  52. private int elementos;
  53.  
  54. @SuppressWarnings("unchecked")
  55. public TabelaHash(int tamanho) {
  56. this.tabela = new ArrayList[tamanho];
  57. for (int i = 0; i < this.tabela.length; i++) {
  58. this.tabela[i] = new ArrayList<Pair<K, V>>();
  59. }
  60. this.elementos = 0;
  61. }
  62.  
  63. public void put(K key, V value) {
  64.  
  65. int hash = this.funcaoHash(key);
  66. Pair<K, V> par = new Pair<K, V>(key, value);
  67.  
  68. if (!this.tabela[hash].contains(par)) {
  69. this.tabela[hash].add(par);
  70. this.elementos++;
  71. } else {
  72. this.replaceValue(hash, key, value);
  73. }
  74.  
  75. System.out.println(this.toString());
  76. }
  77.  
  78. private void replaceValue(int hash, K key, V value) {
  79. boolean finish = false;
  80. int index = 0;
  81. while (index < this.tabela.length && !finish) {
  82. if (this.tabela[hash].get(index).getkey().equals(key)) {
  83. this.tabela[hash].get(index).setValue(value);
  84. finish = true;
  85. }
  86. index++;
  87. }
  88. }
  89.  
  90. public void remove(K key) {
  91. int hash = this.funcaoHash(key);
  92. Pair<K, V> par = new Pair<K, V>(key, null);
  93. if (this.tabela[hash].contains(par)) {
  94. this.tabela[hash].remove(par);
  95. this.elementos--;
  96. }
  97. System.out.println(this.toString());
  98. }
  99.  
  100. @SuppressWarnings("unchecked")
  101. public K[] keys() {
  102. int index = 0;
  103. K[] keys = (K[]) new Object[this.elementos];
  104. for (int i = 0; i < this.tabela.length; i++) {
  105. for (Pair<K, V> par : this.tabela[i]) {
  106. keys[index++] = par.getkey();
  107. }
  108. }
  109. Arrays.sort(keys);
  110. return keys;
  111. }
  112.  
  113. @SuppressWarnings("unchecked")
  114. public V[] values() {
  115. int index = 0;
  116. V[] values = (V[]) new Object[this.elementos];
  117. for (int i = 0; i < this.tabela.length; i++) {
  118. for (Pair<K, V> par : this.tabela[i]) {
  119. values[index++] = par.getValue();
  120. }
  121. }
  122. Arrays.sort(values);
  123. return values;
  124. }
  125.  
  126. public int funcaoHash(K key) {
  127. return (Integer) key % this.tabela.length;
  128. }
  129.  
  130. @Override
  131. public String toString() {
  132. return Arrays.toString(this.tabela);
  133. }
  134.  
  135. }
  136.  
  137. class Pair<K, V> {
  138.  
  139. private K key;
  140. private V value;
  141.  
  142. public Pair(K key, V value) {
  143. this.key = key;
  144. this.value = value;
  145. }
  146.  
  147. public K getkey() {
  148. return this.key;
  149. }
  150.  
  151. public V getValue() {
  152. return this.value;
  153. }
  154.  
  155. public void setKey(K key) {
  156. this.key = key;
  157. }
  158.  
  159. public void setValue(V value) {
  160. this.value = value;
  161. }
  162.  
  163. @SuppressWarnings("unchecked")
  164. @Override
  165. public boolean equals(Object obj) {
  166. if (this == obj)
  167. return true;
  168. if (obj == null)
  169. return false;
  170. if (getClass() != obj.getClass())
  171. return false;
  172. Pair<K, V> other = (Pair<K, V>) obj;
  173. if (key == null) {
  174. if (other.key != null)
  175. return false;
  176. } else if (!key.equals(other.key))
  177. return false;
  178. return true;
  179. }
  180.  
  181. @Override
  182. public String toString() {
  183. return "<" + this.key + ", " + this.value + ">";
  184. }
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement