Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package stack;
- import java.util.HashMap;
- import java.util.LinkedList;
- import java.util.Random;
- class Book1 {
- // private - hermetyzacja
- String author;
- // private - hermetyzacja
- String title;
- public Book1(String author, String title) {
- this.author = author;
- this.title = title;
- }
- public String getAuthor() {
- return author;
- }
- public String getTitle() {
- return title;
- }
- public boolean equals(Object o) {
- Book1 e = (Book1) o;
- return author.equals(e.getAuthor()) && title.equals(e.getTitle());
- }
- public int hashcode() {
- return author.length();
- }
- public String toString() {
- return title + "-" + author;
- }
- }
- class SpeedTest {
- public static void main(String[] args) {
- LinkedList<Book1> milionsOfBooks = new LinkedList<>();
- HashMap<Book1, BookDetails> testMap = new HashMap<>();
- Random random = new Random();
- // Pomysłowy sposób zapełnienia mapy :-)
- for (int k = 0; k < 500000; k++) {
- int numberOfA = random.nextInt(15) + 3;
- int date = random.nextInt(17) + 2000;
- double price = Math.random() * 1000 + 50;
- StringBuilder stringTitle = new StringBuilder();
- StringBuilder stringAuthor = new StringBuilder();
- for (int l = 0; l < numberOfA; l++) {
- stringTitle.append('a');
- stringAuthor.append('a');
- }
- Book1 book = new Book1(stringTitle.toString(), stringAuthor.toString());
- BookDetails bookDetails = new BookDetails(date, price);
- milionsOfBooks.add(book);
- testMap.put(book, bookDetails);
- }
- System.out.println(milionsOfBooks);
- System.out.println(testMap);
- // test objects to Add
- Book1 testBook = new Book1("zzzzz", "zzzzzzz");
- BookDetails testBookDetails = new BookDetails(2000, 50.5);
- // LinkedList time measuring
- long begin = System.currentTimeMillis();
- // trzeba było przetestować metodę o sygnaturze: remove(Object o)
- // a Ty przetestowałaś remove(int index) :) to też jest dobrze, ale w zadaniu chcieli tamtą konkretną z objectem,
- // zmien wiec musisz zapisac sobie gdzies pierwsza dodana do kolekcji książkę, a potem wywołać i przetestowac czas:
- // milionsOfBooks.remove(pierwszaDodanaKsiazka);
- milionsOfBooks.remove(0);
- long end = System.currentTimeMillis();
- System.out.println("Removing first object from LinkedList has taken " + (end - begin) + " ms");
- begin = System.currentTimeMillis();
- // to samo co z usuwaniem pierwszej, chcemy przetestowac remove(Object) a nie remove(int)
- //musisz zapisac sobie gdzies ostatnia dodana do kolekcji książkę, a potem wywołać i przetestowac czas:
- // milionsOfBooks.remove(ostatniaDodanaKsiazka);
- milionsOfBooks.remove(milionsOfBooks.size() - 1);
- end = System.currentTimeMillis();
- System.out.println("Removing last object from LinkedList has taken " + (end - begin) + " ms");
- begin = System.currentTimeMillis();
- // dobrze
- milionsOfBooks.add(0, testBook);
- end = System.currentTimeMillis();
- System.out.println("Adding first object to LinkedList has taken " + (end - begin) + " ms");
- begin = System.currentTimeMillis();
- // bardzo dobrze
- milionsOfBooks.add(milionsOfBooks.size(), testBook);
- end = System.currentTimeMillis();
- System.out.println("Adding last object to LinkedList has taken " + (end - begin) + " ms");
- //HashMap time measuring
- begin = System.currentTimeMillis();
- // to jest pobranie elementu o kluczu 500, a Ty nie masz takiego w kolekcji, to nie jest pobranie
- // elementu nr 500
- // powinnas zapisać sobie jakiś klucz który dodałaś do tej kolekcji (on jest typu Book1, bo zdefiniowalas:
- // HashMap<Book1, BookDetails> testMap = new HashMap<>();
- // czyli klucz typu Book1, wartosc dla klucza typu BookDetails
- // i wtedy zrobic testMap.get(jakisObiektTypuBook1);
- testMap.get(500);
- end = System.currentTimeMillis();
- System.out.println("Finding object by the key in the HashMap has taken " + (end - begin) + " ms");
- begin = System.currentTimeMillis();
- testMap.put(testBook, testBookDetails);
- end = System.currentTimeMillis();
- System.out.println("Adding object to the HashMap has taken " + (end - begin) + " ms");
- begin = System.currentTimeMillis();
- // tutaj to samo co w testMap.get, powinno byc cos w stylu:
- // testMap.remove(jakisObiektTypuBook1);
- testMap.remove(8000000);
- end = System.currentTimeMillis();
- System.out.println("Removing object from HashMap has taken " + (end - begin) + " ms");
- }
- }
- class BookDetails {
- // private
- int dateOfEdition;
- // private
- double price;
- public BookDetails(int dateOfEdition, double price) {
- this.dateOfEdition = dateOfEdition;
- this.price = price;
- }
- public double getPrice() {
- return price;
- }
- public int getDateOfEdition() {
- return dateOfEdition;
- }
- public String toString() {
- return dateOfEdition + "," + price;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement