What is the difference between list and vector in Java?

Difference between ArrayList and Vector

ArrayList and Vector both implements List interface and maintains insertion order.

However, there are many differences between ArrayList and Vector classes that are given below.

ArrayListVector
1] ArrayList is not synchronized.Vector is synchronized.
2] ArrayList increments 50% of current array size if the number of elements exceeds from its capacity.Vector increments 100% means doubles the array size if the total number of elements exceeds than its capacity.
3] ArrayList is not a legacy class. It is introduced in JDK 1.2.Vector is a legacy class.
4] ArrayList is fast because it is non-synchronized.Vector is slow because it is synchronized, i.e., in a multithreading environment, it holds the other threads in runnable or non-runnable state until current thread releases the lock of the object.
5] ArrayList uses the Iterator interface to traverse the elements.A Vector can use the Iterator interface or Enumeration interface to traverse the elements.

Example of Java ArrayList

Let's see a simple example where we are using ArrayList to store and traverse the elements.

Test it Now

Output:

Sonoo Michael James Andy

Example of Java Vector

Let's see a simple example of a Java Vector class that uses the Enumeration interface.

Test it Now

Output:

umesh irfan kumar
Next TopicJava JDBC


← prev next →


Vector vs ArrayList in Java

ArrayList and Vectors both implement the List interface, and both use [dynamically resizable] arrays for their internal data structure, much like using an ordinary array.

Syntax:

ArrayList: ArrayList al = new ArrayList[]; Vector: Vector v = new Vector[];

Vector vs. ArrayList in Java

S. No. ArrayList Vector
1. ArrayList is not synchronized. Vector is synchronized.
2. ArrayList increments 50% of the current array size if the number of elements exceeds ts capacity. Vector increments 100% means doubles the array size if the total number of elements exceeds its capacity.
3. ArrayList is not a legacy class. It is introduced in JDK 1.2. Vector is a legacy class.
4. ArrayList is fast because it is non-synchronized. Vector is slow because it is synchronized, i.e., in a multithreading environment, it holds the other threads in a runnable or non-runnable state until the current thread releases the lock of the object.
5. ArrayList uses the Iterator interface to traverse the elements. A Vector can use the Iterator interface or Enumeration interface to traverse the elements.

Significant Differences between ArrayList and Vector:

  • Synchronization: Vector is synchronized, which means only one thread at a time can access the code, while ArrayList is not synchronized, which means multiple threads can work on ArrayList at the same time. For example, if one thread is performing an add operation, then there can be another thread performing a remove operation in a multithreading environment.If multiple threads access ArrayList concurrently, then we must synchronize the block of the code which modifies the list structurally or allow simple element modifications. Structural modification means the addition or deletion of element[s] from the list. Setting the value of an existing element is not a structural modification.

  • Performance: ArrayList is faster. Since it is non-synchronized, while vector operations give slower performance since they are synchronized [thread-safe], if one thread works on a vector, it has acquired a lock on it, which forces any other thread wanting to work on it to have to wait until the lock is released.
  • Data Growth: ArrayList and Vector both grow and shrink dynamically to maintain optimal use of storage – but the way they resize is different. ArrayList increments 50% of the current array size if the number of elements exceeds its capacity, while vector increments 100% – essentially doubling the current array size.
  • Traversal: Vector can use both Enumeration and Iterator for traversing over vector elements, while ArrayList can only use Iterator for traversing.
  • Applications: Most of the time, programmers prefer ArrayList over Vector because ArrayList can be synchronized explicitly using Collections.synchronizedList.

Note: ArrayList is preferable when there is no specific requirement to use vector.



Java




// Java Program to illustrate use
// of ArrayList and Vector in Java
import java.io.*;
import java.util.*;
class GFG
{
public static void main [String[] args]
{
// creating an ArrayList
ArrayList al = new ArrayList[];
// adding object to arraylist
al.add["Practice.GeeksforGeeks.org"];
al.add["quiz.GeeksforGeeks.org"];
al.add["code.GeeksforGeeks.org"];
al.add["contribute.GeeksforGeeks.org"];
// traversing elements using Iterator'
System.out.println["ArrayList elements are:"];
Iterator it = al.iterator[];
while [it.hasNext[]]
System.out.println[it.next[]];
// creating Vector
Vector v = new Vector[];
v.addElement["Practice"];
v.addElement["quiz"];
v.addElement["code"];
// traversing elements using Enumeration
System.out.println["\nVector elements are:"];
Enumeration e = v.elements[];
while [e.hasMoreElements[]]
System.out.println[e.nextElement[]];
}
}
Output ArrayList elements are: Practice.GeeksforGeeks.org quiz.GeeksforGeeks.org code.GeeksforGeeks.org contribute.GeeksforGeeks.org Vector elements are: Practice quiz code

How to choose between ArrayList and Vector?

  • ArrayList is unsynchronized and not thread-safe, whereas Vectors are. Only one thread can call methods on a Vector, which is slightly overhead but helpful when safety is a concern. Therefore, in a single-threaded case, ArrayList is the obvious choice, but where multithreading is concerned, vectors are often preferable.
  • If we don’t know how much data we will have but know the rate at which it grows, Vector has an advantage since we can set the increment value in vectors.
  • ArrayList is newer and faster. If we don’t have any explicit requirements for using either of them, we use ArrayList over vector.

This article is contributed by Nitsdheerendra. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.




Article Tags :
Java
Java-ArrayList
Java-Collections
java-list
Java-List-Programs
Practice Tags :
Java
Java-Collections
Read Full Article

Difference Between Vector and List

Vector: Vector is a type of dynamic array which has the ability to resize automatically after insertion or deletion of elements. The elements in vector are placed in contiguous storage so that they can be accessed and traversed using iterators. Element is inserted at the end of the vector.
Example:

vector v; v.insert[5]; v.delete[];

List: List is a double linked sequence that supports both forward and backward traversal. The time taken in the insertion and deletion in the beginning, end and middle is constant. It has the non-contiguous memory and there is no pre-allocated memory.
Example:

list l; l.insert_begin[5]; l.delete_end[];

Below is a table of differences between Vector and List:

Vector List
It has contiguous memory. While it has non-contiguous memory.
It is synchronized. While it is not synchronized.
Vector may have a default size. List does not have default size.
In vector, each element only requires the space for itself only. In list, each element requires extra space for the node which holds the element, including pointers to the next and previous elements in the list.
Insertion at the end requires constant time but insertion elsewhere is costly. Insertion is cheap no matter where in the list it occurs.
Vector is thread safe. List is not thread safe.
Deletion at the end of the vector needs constant time but for the rest it is O[n]. Deletion is cheap no matter where in the list it occurs.
Random access of elements is possible. Random access of elements is not possible.
Iterators become invalid if elements are added to or removed from the vector. Iterators are valid if elements are added to or removed from the list.

Article Tags :
Difference Between
GBlog
Read Full Article

Differences between ArrayList and Vector in Java

JavaObject Oriented ProgrammingProgramming

Both ArrayList and Vector are implementation of List interface in Java. Both classes keeps the insertion order. But there are certain differences as well.

Following are the important differences between ArrayList and Vector method.

Sr. No.
Key
ArrayList
Vector
1
Synchronization
ArrayList is non-synchronized.
Vector is synchronized.
2
Size
ArrayList increments 50% of its current size if element added exceeds its capacity.
Vector increments 100% of its current size if element added exceeds its capacity.
3
Legacy
ArrayList is not legacy.
Vector is a legacy class.
4
Speed
ArrayList is faster being non-syncronized.
LinkedList is slower being syncronized.
5
Iteration
ArrayList uses iterator interface to traverse through elements.
Vector can use both iterator or enumerator interface to traverse through elements.

1. Overview

In this tutorial, we're going to focus on the differences between the ArrayList and Vector classes. They both belong to the Java Collections Framework and implement the java.util.List interface.

However, these classes have significant differences in their implementations.

ArrayList vs Vector

The main difference between ArrayList and Vector lies in the pathway through which they store the data and process it. Both the methods let the users perform a series of functions. Programmers prefer to use ArrayList or vector depending upon their requirements. While one is synchronized, the other is non-synchronized. Their expandable capacity varies. They let the user perform from simplest to more complex operations.

ArrayList helps the user to make modifications in the size of the array. ArrayList makes the array shrink or expand based on the user’s requirement. It is different from built-in arrays as built-in arrays do not let the user modify the size of the array. ArrayList could operates on multiple thread synchronously.

Vector is found in java. util package. It supports a dynamic array of elements which means the array is resizable. Vectors belong to the legacy class. Vectors perform thread-safe operations which means a single thread can perform a single operation at a time which tends to make their performance slower.

Video liên quan

Bài mới nhất

Chủ Đề