LinkdedList
요소 개수 업데이트 있을 경우에 사용
- 2중 연결 리스트 구조
- 새로운 데이터를 추가/삭제할 경우 ArrayList, Vector 같은 배열 구조에 비해 압도적으로 성능 우수
- 배열 구조에서 요소 개수 추가흐는 경우 메모리 크기 늘려야하는데 이게 불가할 경우 새로운 요소와 기존 요소를 모두 수용할 수 있는 메모리 새로 할당해서 복붙하여 추가
import java.util.LinkedList;
import java.util.Vector;
public class Main {
public static void main(String[] args) throws InterruptedException {
LinkedList<String> linkedList = new LinkedList<String>();
Vector<String> list = new Vector<String>();
long beginTime = System.currentTimeMillis();
for (int i = 0; i < 200000; i++)
linkedList.add(0, "Test" + i);
long endTime = System.currentTimeMillis();
System.out.println("LinkedList: " + linkedList.size());
System.out.println("Duration: " + (endTime - beginTime) + " ms");
beginTime = System.currentTimeMillis();
for (int i = 0; i < 200000; i++)
list.add(0, "Test" + i);
endTime = System.currentTimeMillis();
System.out.println("Vector: " + list.size());
System.out.println("Duration: " + (endTime - beginTime) + " ms");
}
}실제 실행해 보면 속도 차이가 매우 크다.
LinkedList: 200000
Duration: 31 ms
Vector: 200000
Duration: 1811 ms