Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.16 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.LinkedHashMap;
  3. import java.util.Map;
  4. import java.util.Random;
  5. import java.util.TreeMap;
  6.  
  7. public class Test1{
  8. public static void main(String[] args){
  9. Imie[] imiona = { new Imie("Janusz"),
  10. new Imie("Janek"),
  11. new Imie("Rorat"),
  12. new Imie("Bratek"),
  13. new Imie("Zgredek")};
  14.  
  15. Adres[] adresy = { new Adres("Warszawa"),
  16. new Adres("Kolno"),
  17. new Adres("Stary Sącz"),
  18. new Adres("Nowy Sącz"),
  19. new Adres("Zwykły Sącz")};
  20.  
  21. HashMap<Imie, Adres> hm = new HashMap<>();
  22. LinkedHashMap<Imie, Adres> lhm = new LinkedHashMap<>();
  23. TreeMap<Imie, Adres> tm = new TreeMap<>();
  24.  
  25. for (int i=0; i<imiona.length; i++){
  26. hm.put(imiona[i], adresy[i]);
  27. lhm.put(imiona[i], adresy[i]);
  28. tm.put(imiona[i], adresy[i]);
  29. }
  30.  
  31. System.out.println("Printing HashMap: ");
  32. System.out.println(hm);
  33. System.out.println("\nPrinting LinkedMap: ");
  34. System.out.println(lhm);
  35. System.out.println("\nPrinting TreeMap: ");
  36. System.out.println(tm);
  37. System.out.println("\nKeySet: ");
  38. System.out.println(hm.keySet());
  39. System.out.println("\nValueSet: ");
  40. System.out.println(lhm.values());
  41. System.out.println("\nEntrySet: ");
  42. System.out.println(lhm.entrySet());
  43. System.out.println("\n Janusz's adress: ");
  44. System.out.println(hm.get(imiona[0]));
  45.  
  46. System.out.println("\n Janusz's adress: ");
  47. System.out.println(tm.get(new Imie("Janusz")));
  48.  
  49. System.out.println("\n New Janusz's adress: ");
  50. hm.put(imiona[0], new Adres("Białystok"));
  51. System.out.println(hm.get(imiona[0]));
  52.  
  53. System.out.println("\n Adding new Janusz: ");
  54. hm.put(new Imie("Janusz"), new Adres("Wrocławek"));
  55. System.out.println(hm.entrySet());
  56.  
  57. System.out.println("\n Removing Janusz: ");
  58. hm.remove(imiona[0]);
  59. System.out.println(hm.entrySet());
  60.  
  61. System.out.println("\n Speed Tests: \n");
  62.  
  63. hm.clear();
  64. lhm.clear();
  65. tm.clear();
  66.  
  67. System.out.print("HashMap Put time: ");
  68. wydajnoscPut(hm);
  69. System.out.print("HashMap Get time: ");
  70. wydajnoscGet(hm);
  71. System.out.println();
  72.  
  73. System.out.print("LinkedHashMap Put time: ");
  74. wydajnoscPut(lhm);
  75. System.out.print("LinkedHashMap Get time: ");
  76. wydajnoscGet(lhm);
  77. System.out.println();
  78.  
  79.  
  80. System.out.print("TreeMap Put time: ");
  81. wydajnoscPut(tm);
  82. System.out.print("TreehMap Get time: ");
  83. wydajnoscGet(tm);
  84.  
  85.  
  86. }
  87.  
  88. static void wydajnoscPut(Map<Imie, Adres> m){
  89. Adres adres = new Adres("Test");
  90. Random r = new Random();
  91. long start = System.currentTimeMillis();
  92. for (int i=0; i<100000; i++){
  93. m.put(new Imie(r.nextInt()+""), adres);
  94. }
  95. long end = System.currentTimeMillis();
  96. long duration = end - start;
  97. System.out.println(duration + "ms");
  98. }
  99.  
  100. static void wydajnoscGet(Map<Imie, Adres> m){
  101. Random r = new Random();
  102. long start = System.currentTimeMillis();
  103. for (int i=0; i<100000; i++){
  104. m.get(new Imie(r.nextInt()+""));
  105. }
  106. long end = System.currentTimeMillis();
  107. long duration = end - start;
  108. System.out.println(duration + "ms");
  109. }
  110. }
  111.  
  112. class Imie implements Comparable<Imie>{
  113. private String imie;
  114. public Imie(String imie){
  115. this.imie = imie;
  116. }
  117.  
  118. @Override
  119. public String toString(){
  120. return imie;
  121. }
  122.  
  123. @Override
  124. public int compareTo(Imie inne){
  125. return imie.compareTo(inne.imie);
  126. }
  127. }
  128.  
  129. class Adres{
  130. private String adres;
  131. public Adres(String adres){
  132. this.adres = adres;
  133. }
  134.  
  135. @Override
  136. public String toString(){
  137. return adres;
  138. }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement