HomeJavaDifference between ArrayList and LinkedList

Difference between ArrayList and LinkedList

Get 30 minute iPhone repair with Puls! Schedule your repair now!

ArrayList and LinkedList both implements List interface and maintains insertion order. Both are non synchronized classes.

But there are many differences between ArrayList and LinkedList classes that are given below.

[table id=2 /]

Example of ArrayList and LinkedList in Java

Let’s see a simple example where we are using ArrayList and LinkedList both.

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class ListIteration
{
    private static List<String> arrayList = new ArrayList<>();
    private static List<String> linkedList = new LinkedList<>();

    public static void initializeList(List<String> list, int bufferSize)
    {
        for (int i = 0; i < 50000; i++) { byte[] buffer = null; if (bufferSize > 0)
            {
                buffer = new byte[bufferSize];
            }
            String s = String.valueOf(i);
            list.add(s);
            // avoid buffer to be optimized away
            if (System.currentTimeMillis() == 0)
            {
                System.out.println(buffer);
            }
        }
    }

    public static void bench(List<String> list)
    {
        if (list.contains("bar"))
        {
            System.out.println("bar found");
        }
    }

    public static void main(String[] args) throws Exception
    {
        if (args.length != 2) return;
        
        int bufferSize = Integer.parseInt(args[0]);

        initializeList(arrayList, bufferSize);
        initializeList(linkedList, bufferSize);
        
        System.out.println("init done");
        
        System.out.println("ArrayList: " + arrayList);
        System.out.println("LinkedList: " + linkedList);

    }
}

Get 30 minute iPhone repair with Puls! Schedule your repair now!

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular