fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. class Book{
  6. String title;
  7. int year;
  8.  
  9. // constructor with arguments
  10. public Book(String title, int year){
  11. this.title = title;
  12. this.year = year;
  13. }
  14.  
  15. public String getTitle(){
  16. return title;
  17. }
  18.  
  19. public int getYear() {
  20. return year;
  21. }
  22.  
  23. // toString method used for printout the object
  24. public String toString(){
  25. return "Title: \"" + title + "\", year: " + year ;
  26. }
  27.  
  28. public boolean equals(Object o){
  29. final Book e = (Book) o;
  30. return this.title.equals(e.title) && (year == e.getYear()) ;
  31. }
  32.  
  33. public int hashCode(){
  34. return year;
  35. }
  36.  
  37. }
  38.  
  39. class Operations {
  40. LinkedList<Book> bookcase;
  41.  
  42. public Operations() {
  43. bookcase = new LinkedList<Book>();
  44. }
  45.  
  46. public LinkedList<Book> createdListOfBooks() {
  47. Random theGenerator = new Random();
  48. LinkedList<Book> bookcase = new LinkedList<Book>();
  49.  
  50. for(int n = 0; n < 1200000; n++) {
  51. bookcase.add(new Book("Java" + n, 1990 + theGenerator.nextInt(30)));
  52. }
  53. System.out.println("Quantity of elements in the collection: " + bookcase.size());
  54.  
  55. return bookcase;
  56. }
  57.  
  58. public void timeOfAddList(int n, LinkedList<Book> bookcase) {
  59.  
  60. //Adding element at the collection
  61. long begin = System.nanoTime();
  62. bookcase.add(new Book("Java"+n,1988));
  63. long end = System.nanoTime();
  64.  
  65. //Displaying time of the operation
  66. System.out.println("Adding element " + n + " at the collection has taken: " + (end - begin) + "ns");
  67.  
  68. }
  69.  
  70. public void timeOfRemoveList(int n, LinkedList<Book> bookcase) {
  71.  
  72. //Removing element from the collection
  73. long begin = System.nanoTime();
  74. bookcase.remove(n);
  75. long end = System.nanoTime();
  76.  
  77. //Displaying time of the operation
  78. System.out.println("Removing element no: " + n + " from the collection has taken: " + (end - begin) + "ns");
  79.  
  80. }
  81.  
  82.  
  83. public HashMap<Integer, Book> createdHashMap() {
  84. HashMap<Integer, Book> theMap = new HashMap<Integer, Book>();
  85. Integer tempInt = 0;
  86. for(int n=1; n< 1200000; n++) {
  87. Book book = new Book("Java" + n, n);
  88. tempInt = n;
  89. theMap.put(tempInt, book);
  90. }
  91.  
  92. //Using entrySet() to retrieve and display content of the map
  93. // for(Map.Entry<Integer, String> entry :theMap.entrySet()){
  94. // System.out.println("Object: <" + entry.getKey() + ", " + entry.getValue() + ">");
  95. // }
  96.  
  97. // for(Map.Entry<Employee, SalaryParameters> entry : salariesParameters.entrySet()) {
  98. // if (entry.getValue().baseSalary >= 5000){
  99. // System.out.println("Salary of " + entry.getKey() + " equals " +
  100. // entry.getValue());
  101. // }
  102. // }
  103.  
  104.  
  105. System.out.println("Salary of Sarah Taylor equals: " + theMap.get(9));
  106.  
  107. return theMap;
  108.  
  109. }
  110.  
  111. // System.out.println("Salary of Sarah Taylor equals: " + paymentParameters.get(worker1));
  112.  
  113.  
  114.  
  115. }
  116.  
  117.  
  118. /* Name of the class has to be "Main" only if the class is public. */
  119. class ValidateTimeOfProcessing
  120. {
  121. public static void main (String[] args) throws java.lang.Exception
  122. {
  123. Operations Operate = new Operations();
  124. LinkedList<Book> bookcase = Operate.createdListOfBooks();
  125. Operate.timeOfAddList(0, bookcase);
  126. Operate.timeOfAddList(bookcase.size(), bookcase);
  127. Operate.timeOfRemoveList(1, bookcase);
  128. Operate.timeOfRemoveList(bookcase.size()-1, bookcase);
  129.  
  130. }
  131. }
Success #stdin #stdout 2.68s 4386816KB
stdin
Standard input is empty
stdout
Quantity of elements in the collection: 1200000
Adding element 0 at the collection has taken: 3742ns
Adding element 1200001 at the collection has taken: 797ns
Removing element no: 1 from the collection has taken: 9771ns
Removing element no: 1200000 from the collection has taken: 1221ns