Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.HashMap;
- import java.util.LinkedHashMap;
- import java.util.Map;
- import java.util.Random;
- import java.util.TreeMap;
- public class Test1{
- public static void main(String[] args){
- Imie[] imiona = { new Imie("Janusz"),
- new Imie("Janek"),
- new Imie("Rorat"),
- new Imie("Bratek"),
- new Imie("Zgredek")};
- Adres[] adresy = { new Adres("Warszawa"),
- new Adres("Kolno"),
- new Adres("Stary Sącz"),
- new Adres("Nowy Sącz"),
- new Adres("Zwykły Sącz")};
- HashMap<Imie, Adres> hm = new HashMap<>();
- LinkedHashMap<Imie, Adres> lhm = new LinkedHashMap<>();
- TreeMap<Imie, Adres> tm = new TreeMap<>();
- for (int i=0; i<imiona.length; i++){
- hm.put(imiona[i], adresy[i]);
- lhm.put(imiona[i], adresy[i]);
- tm.put(imiona[i], adresy[i]);
- }
- System.out.println("Printing HashMap: ");
- System.out.println(hm);
- System.out.println("\nPrinting LinkedMap: ");
- System.out.println(lhm);
- System.out.println("\nPrinting TreeMap: ");
- System.out.println(tm);
- System.out.println("\nKeySet: ");
- System.out.println(hm.keySet());
- System.out.println("\nValueSet: ");
- System.out.println(lhm.values());
- System.out.println("\nEntrySet: ");
- System.out.println(lhm.entrySet());
- System.out.println("\n Janusz's adress: ");
- System.out.println(hm.get(imiona[0]));
- System.out.println("\n Janusz's adress: ");
- System.out.println(tm.get(new Imie("Janusz")));
- System.out.println("\n New Janusz's adress: ");
- hm.put(imiona[0], new Adres("Białystok"));
- System.out.println(hm.get(imiona[0]));
- System.out.println("\n Adding new Janusz: ");
- hm.put(new Imie("Janusz"), new Adres("Wrocławek"));
- System.out.println(hm.entrySet());
- System.out.println("\n Removing Janusz: ");
- hm.remove(imiona[0]);
- System.out.println(hm.entrySet());
- System.out.println("\n Speed Tests: \n");
- hm.clear();
- lhm.clear();
- tm.clear();
- System.out.print("HashMap Put time: ");
- wydajnoscPut(hm);
- System.out.print("HashMap Get time: ");
- wydajnoscGet(hm);
- System.out.println();
- System.out.print("LinkedHashMap Put time: ");
- wydajnoscPut(lhm);
- System.out.print("LinkedHashMap Get time: ");
- wydajnoscGet(lhm);
- System.out.println();
- System.out.print("TreeMap Put time: ");
- wydajnoscPut(tm);
- System.out.print("TreehMap Get time: ");
- wydajnoscGet(tm);
- }
- static void wydajnoscPut(Map<Imie, Adres> m){
- Adres adres = new Adres("Test");
- Random r = new Random();
- long start = System.currentTimeMillis();
- for (int i=0; i<100000; i++){
- m.put(new Imie(r.nextInt()+""), adres);
- }
- long end = System.currentTimeMillis();
- long duration = end - start;
- System.out.println(duration + "ms");
- }
- static void wydajnoscGet(Map<Imie, Adres> m){
- Random r = new Random();
- long start = System.currentTimeMillis();
- for (int i=0; i<100000; i++){
- m.get(new Imie(r.nextInt()+""));
- }
- long end = System.currentTimeMillis();
- long duration = end - start;
- System.out.println(duration + "ms");
- }
- }
- class Imie implements Comparable<Imie>{
- private String imie;
- public Imie(String imie){
- this.imie = imie;
- }
- @Override
- public String toString(){
- return imie;
- }
- @Override
- public int compareTo(Imie inne){
- return imie.compareTo(inne.imie);
- }
- }
- class Adres{
- private String adres;
- public Adres(String adres){
- this.adres = adres;
- }
- @Override
- public String toString(){
- return adres;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement