remote87

ArrayList vs LinkedList

Aug 1st, 2021
988
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.company;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.Comparator;
  6. import java.util.HashMap;
  7. import java.util.LinkedList;
  8. import java.util.List;
  9. import java.util.Map;
  10. import java.util.Map.Entry;
  11. import java.util.Scanner;
  12.  
  13. public class Main {
  14.  
  15.     public static void main(String[] args) {
  16.  
  17.         ArrayList<Integer> arrList = new ArrayList<>();
  18.         LinkedList<Integer> linkedList = new LinkedList<>();
  19.  
  20.         for (int i = 0; i < 1_000_000; i++) {
  21.             arrList.add(i);
  22.             linkedList.add(i);
  23.         }
  24.  
  25.         //GETTING AN ELEMENT
  26.  
  27.         //ArrayList
  28.         final long startTimeArLGetEl = System.nanoTime();
  29.         linkedList.get(500_000);
  30.         final long endTimeArLGetEl = System.nanoTime();
  31.         long totalTimeArLGetEl = endTimeArLGetEl - startTimeArLGetEl;
  32.  
  33.         //LinkedList
  34.         final long startTimeLLGetEl = System.nanoTime();
  35.         linkedList.get(500_000);
  36.         final long endTimeLLGetEl = System.nanoTime();
  37.         long totalTimeLLGetEl = endTimeLLGetEl - startTimeArLGetEl;
  38.  
  39.         System.out.println("GETTING AN ELEMENT TIMING: ");
  40.         System.out.println("Arraylist getting time: " + totalTimeArLGetEl);
  41.         System.out.println("LinkedList getting time: " + totalTimeLLGetEl);
  42.  
  43.         winner(totalTimeArLGetEl, totalTimeLLGetEl);
  44.  
  45.  
  46.         //REMOVING AN ELEMENT
  47.  
  48.         //ArrayList
  49.         final long startTimeArLRemoveEl = System.nanoTime();
  50.         linkedList.remove(500_000);
  51.         final long endTimeArLRemoveEl = System.nanoTime();
  52.         long totalTimeArLRemoveEl = endTimeArLRemoveEl - startTimeArLRemoveEl;
  53.  
  54.         //LinkedList
  55.         final long startTimeLLRemoveEl = System.nanoTime();
  56.         linkedList.remove(500_000);
  57.         final long endTimeLLRemoveEl = System.nanoTime();
  58.         long totalTimeLLRemoveEl = endTimeLLRemoveEl - startTimeLLRemoveEl;
  59.  
  60.         System.out.println("REMOVING AN ELEMENT TIMING: ");
  61.         System.out.println("Arraylist removing time: " + totalTimeArLRemoveEl);
  62.         System.out.println("LinkedList removing time: " + totalTimeLLRemoveEl);
  63.  
  64.         winner(totalTimeArLRemoveEl, totalTimeLLRemoveEl);
  65.  
  66.  
  67.         //ADDING AN ELEMENT
  68.  
  69.         //ArrayList
  70.         final long startTimeArLAddEl = System.nanoTime();
  71.         linkedList.add(500_000, 1);
  72.         final long endTimeArLAddEl = System.nanoTime();
  73.         long totalTimeArLAddEl = endTimeArLAddEl - startTimeArLAddEl;
  74.  
  75.         //LinkedList
  76.         final long startTimeLLAddEl = System.nanoTime();
  77.         linkedList.add(500_000, 1);
  78.         final long endTimeLLAddEl = System.nanoTime();
  79.         long totalTimeLLAddEl = endTimeLLAddEl - startTimeLLAddEl;
  80.  
  81.         System.out.println("ADDING AN ELEMENT TIMING: ");
  82.         System.out.println("Arraylist adding time: " + totalTimeArLAddEl);
  83.         System.out.println("LinkedList adding time: " + totalTimeLLAddEl);
  84.  
  85.         winner(totalTimeArLAddEl, totalTimeLLAddEl);
  86.  
  87.  
  88.  
  89.         //ADDING AN ELEMENT AT POSITION 1
  90.  
  91.         //ArrayList
  92.         final long startTimeArLAddElOne = System.nanoTime();
  93.         linkedList.add(0, 500_000);
  94.         final long endTimeArLAddElOne = System.nanoTime();
  95.         long totalTimeArLAddElOne = endTimeArLAddElOne - startTimeArLAddElOne;
  96.  
  97.         //LinkedList
  98.         final long startTimeLLAddElOne = System.nanoTime();
  99.         linkedList.add(0, 500_000);
  100.         final long endTimeLLAddElOne = System.nanoTime();
  101.         long totalTimeLLAddElOne = endTimeLLAddElOne - startTimeLLAddElOne;
  102.  
  103.         System.out.println("ADDING AN ELEMENT TO POSITION 1 TIMING: ");
  104.         System.out.println("Arraylist adding time: " + totalTimeArLAddElOne);
  105.         System.out.println("LinkedList adding time: " + totalTimeLLAddElOne);
  106.  
  107.         winner(totalTimeArLAddElOne, totalTimeLLAddElOne);
  108.  
  109.  
  110.         //REMOVING AN ELEMENT AT POSITION 1
  111.  
  112.         //ArrayList
  113.         final long startTimeArLRemoveElOne = System.nanoTime();
  114.         linkedList.remove(1);
  115.         final long endTimeArLRemoveElOne = System.nanoTime();
  116.         long totalTimeArLRemoveElOne = endTimeArLRemoveElOne - startTimeArLRemoveElOne;
  117.  
  118.         //LinkedList
  119.         final long startTimeLLRemoveElOne = System.nanoTime();
  120.         linkedList.remove(1);
  121.         final long endTimeLLRemoveElOne = System.nanoTime();
  122.         long totalTimeLLRemoveElOne = endTimeLLRemoveElOne - startTimeLLRemoveElOne;
  123.  
  124.         System.out.println("REMOVING AN ELEMENT TO POSITION 1 TIMING: ");
  125.         System.out.println("Arraylist adding time: " + totalTimeArLRemoveElOne);
  126.         System.out.println("LinkedList adding time: " + totalTimeLLRemoveElOne);
  127.  
  128.         winner(totalTimeArLRemoveElOne, totalTimeLLRemoveElOne);
  129.  
  130.     }
  131.  
  132.     public static void winner (long arrListResult, long linkedListResult) {
  133.         System.out.println();
  134.         String winner = "";
  135.         long difference = 0L;
  136.  
  137.         if (arrListResult < linkedListResult) {
  138.             winner = "ARRAY LIST";
  139.             difference = linkedListResult - arrListResult;
  140.         } else {
  141.             winner = "LINKED LIST";
  142.             difference = arrListResult - linkedListResult;
  143.         }
  144.  
  145.         System.out.printf("%s is faster with %d difference!%n", winner, difference);
  146.         System.out.println("-----------------------------------------------------");
  147.     }
  148. }
RAW Paste Data