package com.stulsoft.pjava.basics.collections;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;

public class LinkedArrayListPerformance {
    record Stats(Long arrayList, Long linkedList) {
    }

    public static void main(String[] args) {
        var collection = generate(10_000_000);
        var stats = new ArrayList<Stats>();
        for (int i = 1; i < 100; ++i)
            stats.add(new Stats(testArrayList(collection), testLinkedList(collection)));
        int averageArrayList = 0;
        int averageLinkedList = 0;
        for (Stats stat : stats) {
            System.out.println(stat);
            averageArrayList += stat.arrayList();
            averageLinkedList += stat.linkedList();
        }
        System.out.printf("averageArrayList = %d, averageLinkedList = %d%n",
                averageArrayList / stats.size(),
                averageLinkedList / stats.size());
    }

    private static Long testArrayList(Collection<String> data) {
        long start = System.currentTimeMillis();
//        new ArrayList<>(data);
        var list = new ArrayList<String>();
        list.addAll(data);
        return System.currentTimeMillis() - start;
    }

    private static Long testLinkedList(Collection<String> data) {
        long start = System.currentTimeMillis();
//        new LinkedList<>(data);
        var list = new LinkedList<String>();
        list.addAll(data);
        return System.currentTimeMillis() - start;
    }

    private static Collection<String> generate(int n) {
        ArrayList<String> list = new ArrayList<>();
        for (int i = 1; i <= n; ++i) {
            list.add(String.format("Text %d", i));
        }
        return list;
    }
}

@@ -297,26 +297,20 @@
     var 
-collection
+data
  = gener
@@ -323,24 +323,190 @@
 _000_000);%0D%0A
+        testAddAll(data);%0D%0A        testAdd(data);%0D%0A    %7D%0D%0A%0D%0A    private static void testAddAll(Collection%3CString%3E data)%7B%0D%0A        System.out.println(%22testAddAll:%22);%0D%0A
         var 
@@ -610,24 +610,30 @@
  Stats(testA
+ddAllA
 rrayList(col
@@ -633,46 +633,760 @@
 ist(
-collection), testLinkedList(collection
+data), testAddAllLinkedList(data)));%0D%0A        int averageArrayList = 0;%0D%0A        int averageLinkedList = 0;%0D%0A        for (Stats stat : stats) %7B%0D%0A//            System.out.println(stat);%0D%0A            averageArrayList += stat.arrayList();%0D%0A            averageLinkedList += stat.linkedList();%0D%0A        %7D%0D%0A        System.out.printf(%22averageArrayList = %25d, averageLinkedList = %25d%25n%22,%0D%0A                averageArrayList / stats.size(),%0D%0A                averageLinkedList / stats.size());%0D%0A    %7D%0D%0A%0D%0A    private static void testAdd(Collection%3CString%3E data)%7B%0D%0A        System.out.println(%22testAdd:%22);%0D%0A        var stats = new ArrayList%3CStats%3E();%0D%0A        for (int i = 1; i %3C 100; ++i)%0D%0A            stats.add(new Stats(testAddArrayList(data), testAddLinkedList(data
 )));
@@ -1490,24 +1490,26 @@
 : stats) %7B%0D%0A
+//
             
@@ -1868,16 +1868,22 @@
 ng testA
+ddAllA
 rrayList
@@ -1965,42 +1965,8 @@
 );%0D%0A
-//        new ArrayList%3C%3E(data);%0D%0A
     
@@ -2123,16 +2123,542 @@
 ong test
+AddArrayList(Collection%3CString%3E data) %7B%0D%0A        long start = System.currentTimeMillis();%0D%0A        var list = new ArrayList%3CString%3E();%0D%0A        data.forEach(list::add);%0D%0A        return System.currentTimeMillis() - start;%0D%0A    %7D%0D%0A%0D%0A    private static Long testAddAllLinkedList(Collection%3CString%3E data) %7B%0D%0A        long start = System.currentTimeMillis();%0D%0A        var list = new LinkedList%3CString%3E();%0D%0A        data.forEach(list::add);%0D%0A        return System.currentTimeMillis() - start;%0D%0A    %7D%0D%0A%0D%0A    private static Long testAdd
 LinkedLi
@@ -2742,43 +2742,8 @@
 );%0D%0A
-//        new LinkedList%3C%3E(data);%0D%0A
     

add one by one