Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4584436
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T21:21:17+00:00 2026-05-21T21:21:17+00:00

I was following a previous post on this that says: For LinkedList get is

  • 0

I was following a previous post on this that says:

For LinkedList

  • get is O(n)
  • add is O(1)
  • remove is O(n)
  • Iterator.remove is O(1)

For ArrayList

  • get is O(1)
  • add is O(1) amortized, but O(n) worst-case since the array must be resized and copied
  • remove is O(n)

So by looking at this, I concluded that if I’ve to do just sequential insert in my collection for say 5000000 elements, LinkedList will outclass ArrayList.

And if I’ve to just fetch the elements from collection by iterating i.e. not grabbing the element in middle, still LinkedList will outclass ArrayList.

Now to verify my above two statements, I wrote below sample program… But I’m surprised that my above statements were proven wrong.

ArrayList outclassed Linkedlist in both the cases. It took less time than LinkedList for adding as well as fetching them from Collection. Is there anything I’m doing wrong, or the initial statements about LinkedList and ArrayList does not hold true for collections of size 5000000?

I mentioned size, because if I reduce the number of elements to 50000, LinkedList performs better and initial statements hold true.

long nano1 = System.nanoTime();

List<Integer> arr = new ArrayList();
for (int i = 0; i < 5000000; i++) {
    arr.add(i);
}
System.out.println(System.nanoTime() - nano1);

for (int j : arr) {
    // Do nothing
}
System.out.println(System.nanoTime() - nano1);

long nano2 = System.nanoTime();

List<Integer> arrL = new LinkedList();
for (int i = 0; i < 5000000; i++) {
    arrL.add(i);
}
System.out.println(System.nanoTime() - nano2);

for (int j : arrL) {
    // Do nothing
}
System.out.println(System.nanoTime() - nano2);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T21:21:18+00:00Added an answer on May 21, 2026 at 9:21 pm

    Remember that big-O complexity describes asymptotic behaviour and may not reflect actual implementation speed. It describes how the cost of each operation grows with the size of the list, not the speed of each operation. For example, the following implementation of add is O(1) but is not fast:

    public class MyList extends LinkedList {
        public void add(Object o) {
            Thread.sleep(10000);
            super.add(o);
        }
    }
    

    I suspect in your case ArrayList is performing well because it increases it’s internal buffer size fairly aggressively so there will not be a large number of reallocations. When the buffer does not need to be resized ArrayList will have faster adds.

    You also need to be very careful when you do this kind of profiling. I’d suggest you change your profiling code to do a warm-up phase (so the JIT has the opportunity to do some optimization without affecting your results) and average the results over a number of runs.

    private final static int WARMUP = 1000;
    private final static int TEST = 1000;
    private final static int SIZE = 500000;
    
    public void perfTest() {
        // Warmup
        for (int i = 0; i < WARMUP; ++i) {
            buildArrayList();
        }
        // Test
        long sum = 0;
        for (int i = 0; i < TEST; ++i) {
            sum += buildArrayList();
        }
        System.out.println("Average time to build array list: " + (sum / TEST));
    }
    
    public long buildArrayList() {
        long start = System.nanoTime();
        ArrayList a = new ArrayList();
        for (int i = 0; i < SIZE; ++i) {
            a.add(i);
        }
        long end = System.nanoTime();
        return end - start;
    }
    
    ... same for buildLinkedList
    

    (Note that sum may overflow and you might be better to use System.currentTimeMillis()).

    It’s also possible that the compiler is optimizing away your empty get loops. Make sure the loop actually does something to ensure that the right code is getting called.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In a previous post ( Don't show this if the url contains the following
Related to my previous post but its not a duplicate of that.Now I have
Following my previous post in this link I have another problem . Given the
in my previous post I was asking how to generate numbers following a normal
this is an extension of my previous thread . I thought that accessing a
This question is following a previous one which i posted: Django Callback on Facebook
This is related to a previous question , but it seems different enough to
I don't know if the Title of this post makes sense, but here's a
This is related to my previous post Problem with downloading multiple files using AsyncTask
I'm experiencing the same problem in this previous stackoverflow.com post . Specifically, I seem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.