Guest User

Untitled

a guest
Jun 22nd, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.33 KB | None | 0 0
  1. public interface IDictionary {
  2.  
  3. /**
  4. * Помещает значение в ячейку с указанным ключем
  5. * @param key - ключ
  6. * @param value - значение
  7. * @return true - если помещение успешно, иначе - false
  8. */
  9. boolean put(String key, String value);
  10.  
  11. /**
  12. * @param key - ключ
  13. * @return значение ассоциированное с указанным ключем
  14. */
  15. String get(String key);
  16.  
  17. /**
  18. * Удаляет из структуры значение с указанным ключем
  19. * @param key - ключ
  20. * @return true - если удаление успешно, иначе - false
  21. */
  22. boolean remove(String key);
  23.  
  24. /**
  25. * Проверяет структуру на пустоту
  26. * @return true - если в структуре нет значений, иначе false
  27. */
  28. boolean isEmpty();
  29. }
  30.  
  31.  
  32. import java.util.HashMap;
  33. import java.util.Map;
  34.  
  35. public class Dictionary implements IDictionary {
  36.  
  37. private Node root = new Node();
  38.  
  39. private static class Node {
  40. Map<Character, Node> children = new HashMap<>();
  41. Node parent = null;
  42. String value = null;
  43. }
  44.  
  45. @Override
  46. public boolean put(String key, String value) {
  47. try {
  48. Node currentNode = root;
  49. Node currentParent;
  50.  
  51. for(char character : key.toCharArray()) {
  52. if (!currentNode.children.containsKey(character)) {
  53. currentNode.children.put(character, new Node());
  54. }
  55.  
  56. currentParent = currentNode;
  57. currentNode = currentNode.children.get(character);
  58.  
  59. currentNode.parent = currentParent;
  60. }
  61.  
  62. currentNode.value = value;
  63.  
  64. return true;
  65.  
  66. } catch (Exception e) {
  67. System.out.println("Добавление не удалось. Причина: " + e.getMessage());
  68. return false;
  69. }
  70. }
  71.  
  72. @Override
  73. public String get(String key) {
  74. Node currentNode = root;
  75.  
  76. for (char character : key.toCharArray()) {
  77. if (!currentNode.children.containsKey(character)) {
  78. return null;
  79. } else {
  80. currentNode = currentNode.children.get(character);
  81. }
  82. }
  83.  
  84. return currentNode.value;
  85. }
  86.  
  87. @Override
  88. public boolean remove(String key) {
  89. try {
  90.  
  91. Node currentNode = root;
  92.  
  93. for (char character : key.toCharArray()) {
  94. if (!currentNode.children.containsKey(character)) {
  95. return false;
  96. } else {
  97. currentNode = currentNode.children.get(character);
  98. }
  99. }
  100.  
  101. currentNode.value = null;
  102.  
  103. while (currentNode.parent != null && currentNode.children.size() == 0) {
  104. Node currentParent = currentNode.parent;
  105. if (currentParent.children.size() == 1 && currentNode.value == null) {
  106. currentParent.children.clear();
  107. currentNode = currentParent;
  108. }
  109. else {
  110. break;
  111. }
  112. }
  113.  
  114. return true;
  115.  
  116. } catch (Exception e) {
  117. System.out.println("Удаление не удалось. Причина: " + e.getMessage());
  118. return false;
  119. }
  120. }
  121.  
  122. @Override
  123. public boolean isEmpty() {
  124. return root.children.isEmpty();
  125. }
  126. }
  127.  
  128.  
  129. import java.util.ArrayList;
  130. import java.util.HashMap;
  131. import java.util.List;
  132. import java.util.Random;
  133.  
  134. public class Main {
  135.  
  136. private static final int NUMBER_OF_ELEMENTS = 1000000;
  137.  
  138. public static void main(String[] args) {
  139.  
  140. Dictionary dictionary = new Dictionary();
  141.  
  142. HashMap<String, String> map = new HashMap<>();
  143.  
  144. List<String> keys = new ArrayList<>();
  145. List<String> values = new ArrayList<>();
  146.  
  147. for (int i = 0; i < NUMBER_OF_ELEMENTS; i++) {
  148. keys.add(getRandomString(10));
  149. values.add(getRandomString(50));
  150. }
  151.  
  152. long start = System.currentTimeMillis();
  153. for (int i = 0; i < keys.size(); i++) {
  154. dictionary.put(keys.get(i), values.get(i));
  155. }
  156. long end = System.currentTimeMillis();
  157.  
  158. System.out.println("Время добавления в Dictionary: " + (end - start) + " мсекунд.");
  159.  
  160. start = System.currentTimeMillis();
  161. for (int i = 0; i < keys.size(); i++) {
  162. map.put(keys.get(i), values.get(i));
  163. }
  164. end = System.currentTimeMillis();
  165.  
  166. System.out.println("Время добавления в HashMap: " + (end - start) + " мсекунд.");
  167.  
  168. start = System.currentTimeMillis();
  169. for (int i = 0; i < keys.size(); i++) {
  170. dictionary.get(keys.get(i));
  171. }
  172. end = System.currentTimeMillis();
  173.  
  174. System.out.println("Время взятия элемента в Dictionary: " + (end - start) + " мсекунд.");
  175.  
  176. start = System.currentTimeMillis();
  177. for (int i = 0; i < keys.size(); i++) {
  178. map.get(keys.get(i));
  179. }
  180. end = System.currentTimeMillis();
  181.  
  182. System.out.println("Время взятия элемента в HashMap: " + (end - start) + " мсекунд.");
  183.  
  184. start = System.currentTimeMillis();
  185. for (int i = 0; i < keys.size(); i++) {
  186. dictionary.remove(keys.get(i));
  187. }
  188. end = System.currentTimeMillis();
  189.  
  190. System.out.println("Время удаления элемента в Dictionary: " + (end - start) + " мсекунд.");
  191.  
  192. start = System.currentTimeMillis();
  193. for (int i = 0; i < keys.size(); i++) {
  194. map.remove(keys.get(i));
  195. }
  196. end = System.currentTimeMillis();
  197.  
  198. System.out.println("Время удаления элемента в HashMap: " + (end - start) + " мсекунд.");
  199.  
  200. }
  201.  
  202. private static String getRandomString(int length) {
  203. String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
  204. StringBuilder sb = new StringBuilder();
  205. Random rnd = new Random();
  206.  
  207. while(sb.length() < length)
  208. {
  209. int index = (int) (rnd.nextFloat() * chars.length());
  210. sb.append(chars.charAt(index));
  211. }
  212.  
  213. return sb.toString();
  214. }
  215. }
Add Comment
Please, Sign In to add comment