Java LinkedList vs ArrayList benchmark for adding element

Description

Java LinkedList vs ArrayList benchmark for adding element



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

public class Main {
  public static void main(String... args) {
    int Count = 99999;
    List<Integer> linkedList = new LinkedList<Integer>();
    List<Integer> arrayList = new ArrayList<Integer>();
    long time = System.nanoTime();

    for (int i = 0; i < Count; i++) {
      linkedList.add(Count / 2);/*www  .  jav  a 2  s .  co m*/
    }
    System.out.println("LinkedList add used:" + (System.nanoTime() - time));

    time = System.nanoTime();
    for (int i = 0; i < Count; i++) {
      arrayList.add(Count / 2);
    }
    System.out.println("ArrayList add used:" + (System.nanoTime() - time));

   
  }
}



PreviousNext

Related