| sitelink1 | |
|---|---|
| sitelink2 | |
| sitelink3 | |
| sitelink4 | |
| sitelink5 | |
| sitelink6 | 
Vector 는 thread safe 하다, 하지만 ArrayList 는 thread safe 하지 않다.
thread safe 한 ArrayList 를 구하고 싶으면 Collections.synchronizedCollection 메서드를 사용하면 된다.
List list = Collections.synchronizedList(new ArrayList());
프로그램을 이미 ArrayList 로 구현한 경우 위와 같은 코드로 thread safe 한 ArrayList 로 변형할 수 있다.
Question
Vector or ArrayList -- which is better and why?
Answer
API 
In The Java Programming Language (Addison-Wesley, June 2000) Ken Arnold, James Gosling, and David Holmes describe the Vector as an analog to the ArrayList. So, from an API perspective, the two classes are very similar. However, there are still some major differences between the two classes.
Synchronization 
Vectors are synchronized. Any method that touches the Vector's contents is thread safe. ArrayList, on the other hand, is unsynchronized, making them, therefore, not thread safe. With that difference in mind, using synchronization will incur a performance hit. So if you don't need a thread-safe collection, use the ArrayList. Why pay the price of synchronization unnecessarily?
Data growth 
Internally, both the ArrayList and Vector hold onto their contents using an Array. You need to keep this fact in mind while using either in your programs. When you insert an element into an ArrayList or a Vector, the object will need to expand its internal array if it runs out of room. A Vector defaults to doubling the size of its array, while the ArrayList increases its array size by 50 percent. Depending on how you use these classes, you could end up taking a large performance hit while adding new elements. It's always best to set the object's initial capacity to the largest capacity that your program will need. By carefully setting the capacity, you can avoid paying the penalty needed to resize the internal array later. If you don't know how much data you'll have, but you do know the rate at which it grows, Vector does possess a slight advantage since you can set the increment value.
Usage patterns 
Both the ArrayList and Vector are good for retrieving elements from a specific position in the container or for adding and removing elements from the end of the container. All of these operations can be performed in constant time -- O(1). However, adding and removing elements from any other position proves more expensive -- linear to be exact: O(n-i), where n is the number of elements and i is the index of the element added or removed. These operations are more expensive because you have to shift all elements at index i and higher over by one element. So what does this all mean?
It means that if you want to index elements or add and remove elements at the end of the array, use either a Vector or an ArrayList. If you want to do anything else to the contents, go find yourself another container class. For example, the LinkedList can add or remove an element at any position in constant time -- O(1). However, indexing an element is a bit slower -- O(i) where i is the index of the element. Traversing an ArrayList is also easier since you can simply use an index instead of having to create an iterator. The LinkedList also creates an internal object for each element inserted. So you have to be aware of the extra garbage being created.
Finally, in "PRAXIS 41" from Practical Java (Addison-Wesley, Feb. 2000) Peter Haggar suggests that you use a plain old array in place of either Vector or ArrayList -- especially for performance-critical code. By using an array you can avoid synchronization, extra method calls, and suboptimal resizing. You just pay the cost of extra development time."
댓글 0
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 수 | 
|---|---|---|---|---|
| 66 | 서블릿의 생명주기   | 황제낙엽 | 2006.08.07 | 612 | 
| 65 | 서블릿의 초기화 | 황제낙엽 | 2006.07.24 | 547 | 
| 64 | JSTL | 황제낙엽 | 2006.02.17 | 721 | 
| 63 | J2SE 5.0 Tiger 에 관해서 | 황제낙엽 | 2006.02.16 | 541 | 
| 62 | Building an eBay Rich Client using the XUI Framework | 황제낙엽 | 2006.01.13 | 568 | 
| » | Vector 와 ArrayList의 차이 | 황제낙엽 | 2006.02.15 | 645 | 
| 60 | Using RSS in JSP pages (Informa Project) | 황제낙엽 | 2006.01.10 | 41700 | 
| 59 | 달력만들기 | 황제낙엽 | 2005.12.22 | 786 | 
| 58 | Velocity의 GenericTools 에 있는 DateTool | 황제낙엽 | 2005.12.21 | 586 | 
| 57 | StringTokenizer 예제소스 | 황제낙엽 | 2005.12.21 | 740 | 
| 56 | 유니코드로 된 파일 이름을 인터넷 익스플로러에서 저장하는 방법 | 황제낙엽 | 2005.12.01 | 701 | 
| 55 | Date 클래스와 Calendar 클래스 사이에... | 황제낙엽 | 2005.12.14 | 509 | 
| 54 | 한글처리2 | 황제낙엽 | 2005.12.01 | 573 | 
| 53 | 한글처리1 | 황제낙엽 | 2005.10.20 | 684 | 
| 52 | 유니코드 관련 유틸 클래스 | 황제낙엽 | 2005.07.16 | 789 | 
| 51 | 음력 계산 로직 | 황제낙엽 | 2005.07.16 | 583 | 
| 50 | 서블릿 내장 객체 (Implicit Object) | 황제낙엽 | 2004.11.12 | 626 | 
| 49 | java.text.SimpleDateFormat 클래스를 이용하여 java.util.Date 의 객체 생성시 초기화하기 | 황제낙엽 | 2004.10.03 | 759 | 
| 48 | 서블릿에서 페이지출력 | 황제낙엽 | 2004.09.30 | 531 | 
| 47 | Web Server Page작성시 한글처리를 위한 참고사항 (자바서비스넷 링크문서) | 황제낙엽 | 2004.05.27 | 706 | 
 
							