«««< HEAD
Java collections framework
The Java collections framework is a set of classes and interfaces that implement commonly reusable collection data structures. Although referred to as a framework, it works in a manner of a library. The collections framework provides both interfaces that define various collections and classes that implement them.

import java.util.*; 
class Test { 
    public static void main(String[] arg) 
    { 
  
        // create default vector 
        Vector v = new Vector(); 
  
        v.add(1); 
        v.add(2); 
        v.add("geeks"); 
        v.add("forGeeks"); 
        v.add(3); 
  
        System.out.println("Vector is " + v); 
    } 
}
=======
# LinkedList



```cs
import java.util.*; 
  
public class Test 
{ 
    public static void main(String args[]) 
    { 
        // Creating object of class linked list 
        LinkedList<String> object = new LinkedList<String>(); 
  
        // Adding elements to the linked list 
        object.add("A"); 
        object.add("B"); 
        object.addLast("C"); 
        object.addFirst("D"); 
        object.add(2, "E"); 
        object.add("F"); 
        object.add("G"); 
        System.out.println("Linked list : " + object); 
  
        // Removing elements from the linked list 
        object.remove("B"); 
        object.remove(3); 
        object.removeFirst(); 
        object.removeLast(); 
        System.out.println("Linked list after deletion: " + object); 
  
        // Finding elements in the linked list 
        boolean status = object.contains("E"); 
  
        if(status) 
            System.out.println("List contains the element 'E' "); 
        else
            System.out.println("List doesn't contain the element 'E'"); 
  
        // Number of elements in the linked list 
        int size = object.size(); 
        System.out.println("Size of linked list = " + size); 
  
        // Get and set elements from linked list 
        Object element = object.get(2); 
        System.out.println("Element returned by get() : " + element); 
        object.set(2, "Y"); 
        System.out.println("Linked list after change : " + object); 
    } 
} 
>>>>>>> 6a8ab3d33467e9789826cca739a3f98cfcd27b07

Output:

<<<<<<< HEAD
[1, 2, geeks, forGeeks, 3]

Vector는 ArrayList와 비슷하지만 동기화(synchronize)되어 있다는 차이가 있다.

문법은 거의 동일하므로 두 개의 차이점을 위주로 기술하겠다.

Synchronization

Vector는 동기화되었기 때문에 한번에 한 스레드에만 접속할 수 있지만,

ArrayList는 동기화되지 않으므로 여러 스레드가 동시에 일을 할 수 있다.

예를 들어, 멀티 스레딩 환경에서, 한 스레드가 add 작업을 하는 동안 다른 스레드가 remove 작업을 할 수 있다.

여러 스레드가 동시에 Arraylist에 access하는 경우 경우 리스트를 수정하는 코드 블록을 동기화하던가, 간단한 변경만을 허용하도록 해야 한다.

vector

Performance

Vector가 동기화(thread-safe)되었기 때문에 ArrayList에 비해 더 느리다.

만약 스레드가 Vector에 작업을 하고 있으면 다른 스레드들은 lock이 풀릴 때까지 대기한다.

Data Growth

ArrayList와 Vector 모두 동적으로 사이즈가 늘어나거나 줄어들지만 resize의 방식이 다르다.

ArrayList는 현재 사이즈의 50%씩 늘리며 Vector는 현재 사이즈의 100%, 즉 두배로 늘린다.

Traversal

Vector는 벡터 요소를 순회(traversal)하는 데 Enumeration(열거)과 Iterator(반복자)를 모두 사용할 수 있지만 ArrayList는 순회를 위해 Iterator 만 사용할 수 있다.

Linked list : [D, A, E, B, C, F, G] Linked list after deletion: [A, E, F] List contains the element ‘E’ Size of linked list = 3 Element returned by get() : F Linked list after change : [A, E, Y]


---

LinkedList는 element(**node**)가 연속된 위치에 저장되지 않는 선형 데이터 구조이다.

모든 노드가 주소부와 데이터 부분이 있는 별도의 객체이며 포인터와 주소를 사용하여 연결된다.

동적이고 삽입과 삭제가 자유로운 점에서 array보다 선호될 때가 있다.

head부터 시작하여 링크를 따라 노드에 access해야만 한다.

순환자를 어떻게 사용해야 하는지 잘 보여주는 예시이기도 하다.

```cs
LinkedList myLL = new LinkedList();
myLL.add("two");
myLL.addFirst("one");
Iterator iter = myLL.iterator();
while (iter.hasNext()) {
    System.out.println(iter.next());
}

6a8ab3d33467e9789826cca739a3f98cfcd27b07