What is the difference between ArrayList and LinkedList?

Posted by tommynanda on Wed, 20 Nov 2019 03:51:30 +0100

  • ArrayList is a collection of non-thread-safe implementations based on dynamic arrays; LinkedList is a collection of non-thread-safe implementations based on chain lists.
  • ArrayList is generally faster than LinkedList for get and set methods accessed by random index.Because ArrayList finds elements directly through array subscripts; LinkedList moves the pointer through each element until it is found.
  • LinkedList is generally faster than ArrayList in adding and deleting elements.Because ArrayList can expand and copy arrays when adding and deleting elements; LinkedList instantiates objects in addition to time, it only needs to modify the pointer.
  • LinkedList collection does not support efficient RandomAccess
  • ArrayList's space waste is mainly reflected in reserving a certain amount of space at the end of the list list list, while LinkedList's space expenditure is reflected in the fact that each of its elements consumes a considerable amount of space

 

Test Code

public static void main(String[] args) {
		ArrayList<Integer> arrayList = new ArrayList<Integer>();
		LinkedList<Integer> linkedList = new LinkedList<Integer>();
		int size = 10000 * 1000;
		int index = 5000 * 1000;
		
		System.out.println("arrayList add " + size);
		addData(arrayList, size);
		System.out.println("linkedList add " +  + size);
		addData(linkedList, size);
		System.out.println();
		
		System.out.println("arrayList get " + index + " th");
		getIndex(arrayList, index);
		System.out.println("linkedList get " + index + " th");
		getIndex(linkedList, index);
		System.out.println();
		
		System.out.println("arrayList set " + index + " th");
		setIndex(arrayList, index);
		System.out.println("linkedList set " + index + " th");
		setIndex(linkedList, index);
		System.out.println();
		
		System.out.println("arrayList add " + index + " th");
		addIndex(arrayList, index);
		System.out.println("linkedList add " + index + " th");
		addIndex(linkedList, index);
		System.out.println();
		
		System.out.println("arrayList remove " + index + " th");
		removeIndex(arrayList, index);
		System.out.println("linkedList remove " + index + " th");
		removeIndex(linkedList, index);
		System.out.println();
		
		System.out.println("arrayList remove Object " + index);
		removeObject(arrayList, (Object)index);
		System.out.println("linkedList remove Object " + index);
		removeObject(linkedList, (Object)index);
		System.out.println();
		
		System.out.println("arrayList add");
		add(arrayList);
		System.out.println("linkedList add");
		add(linkedList);
		System.out.println();
		
		System.out.println("arrayList foreach");
		foreach(arrayList);
		System.out.println("linkedList foreach");
		foreach(linkedList);
		System.out.println();
		
		System.out.println("arrayList forSize");
		forSize(arrayList);
		System.out.println("linkedList forSize");
//		forSize(linkedList);
		System.out.println("cost time: ...");
		System.out.println();
		
		System.out.println("arrayList iterator");
		ite(arrayList);
		System.out.println("linkedList iterator");
		ite(linkedList);
	}
	
	private static void addData(List<Integer> list, int size) {
		long s1 = System.currentTimeMillis();
		for (int i = 0; i <size; i++) {
			list.add(i);
		}
		long s2 = System.currentTimeMillis();
		System.out.println("cost time: " + (s2-s1));
	}
	
	private static void getIndex(List<Integer> list, int index) {
		long s1 = System.currentTimeMillis();
		list.get(index);
		long s2 = System.currentTimeMillis();
		System.out.println("cost time: " + (s2-s1));
	}
	
	private static void setIndex(List<Integer> list, int index) {
		long s1 = System.currentTimeMillis();
		list.set(index, 1024);
		long s2 = System.currentTimeMillis();
		System.out.println("cost time: " + (s2-s1));
	}
	
	private static void addIndex(List<Integer> list, int index) {
		long s1 = System.currentTimeMillis();
		list.add(index, 1024);
		long s2 = System.currentTimeMillis();
		System.out.println("cost time: " + (s2-s1));
	}
	
	private static void removeIndex(List<Integer> list, int index) {
		long s1 = System.currentTimeMillis();
		list.remove(index);
		long s2 = System.currentTimeMillis();
		System.out.println("cost time: " + (s2-s1));
	}
	
	private static void removeObject(List<Integer> list, Object obj) {
		long s1 = System.currentTimeMillis();
		list.remove(obj);
		long s2 = System.currentTimeMillis();
		System.out.println("cost time: " + (s2-s1));
	}
	
	private static void add(List<Integer> list) {
		long s1 = System.currentTimeMillis();
		list.add(1024);
		long s2 = System.currentTimeMillis();
		System.out.println("cost time: " + (s2-s1));
	}
	
	private static void foreach(List<Integer> list) {
		long s1 = System.currentTimeMillis();
		for (Integer i : list) {
			//do nothing
		}
		long s2 = System.currentTimeMillis();
		System.out.println("cost time: " + (s2-s1));
	}
	
	private static void forSize(List<Integer> list) {
		long s1 = System.currentTimeMillis();
		int size = list.size();
		for (int i = 0; i <size; i++) {
			list.get(i);
		}
		long s2 = System.currentTimeMillis();
		System.out.println("cost time: " + (s2-s1));
	}
	
	private static void ite(List<Integer> list) {
		long s1 = System.currentTimeMillis();
		Iterator<Integer> ite = list.iterator();
		while (ite.hasNext()) {
			ite.next();
		}
		long s2 = System.currentTimeMillis();
		System.out.println("cost time: " + (s2-s1));
	}

 

JDK1.8, win7 64 bit.Result

arrayList add 10000000
cost time: 3309
linkedList add 10000000
cost time: 1375
 
arrayList get 5000000 th
cost time: 0
linkedList get 5000000 th
cost time: 53
 
arrayList set 5000000 th
cost time: 0
linkedList set 5000000 th
cost time: 44
 
arrayList add 5000000 th
cost time: 3
linkedList add 5000000 th
cost time: 45
 
arrayList remove 5000000 th
cost time: 3
linkedList remove 5000000 th
cost time: 46
 
arrayList remove Object 5000000
cost time: 31
linkedList remove Object 5000000
cost time: 131
 
arrayList add
cost time: 0
linkedList add
cost time: 0
 
arrayList foreach
cost time: 30
linkedList foreach
cost time: 128
 
arrayList forSize
cost time: 5
linkedList forSize
cost time: ...
 
arrayList iterator
cost time: 6
linkedList iterator
cost time: 113

 

Reflection:

  • arrayList add 10000000 cost time: 3293;linkedList add 10000000 cost time: 1337
  • arrayList add 1000000  cost time: 22  ;   linkedList add 1000000   cost time: 1011
  • Running through another set of data, with size set to 1000 * 1000, shows that when size increases, the cumulative time for the add operation of ArrayList increases faster
  • Never call the get method of LinkedList in a loop, it will take you time to crash
  • In the code example, "Adding and deleting elements, LinkedList is generally faster than ArrayList" doesn't work. Think about why.
     

Source analysis reference:


 

 

All Resource Resources Summarized on Public Number



 

Topics: Programming Java