Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
ArrayList vs LinkedList in Java
Next article icon

Vector vs ArrayList in Java

Last Updated : 16 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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<T> al = new ArrayList<T>();    Vector: Vector<T> v = new Vector<T>(); 

ArrayList Vs vector 

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.
6 ArrayList performance is high  Vector performance is low 
7 Multiple threads is allowed  only one threads are allowed .

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.

ArrayList vs Vector Java

  • 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.

How to get Synchronized version of arraylist object:

by default arraylist is object is non-synchronized but we can get synchronized version of arraylist by using collection class Synchronized List() method.

Java




/*package whatever //do not write package name here */
  
import java.io.*;
  
class GFG {
    public static void main (String[] args) {
        public static List SynchronizedList(list1)
          // non synchronized
          ArrayList l1 = new ArrayList();
          // Synchronized
          List l= Collections.SynchronizedList(l1);
    }
}
 
 

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<String> al = new ArrayList<String>();
  
        // adding object to arraylist
        al.add("Practice.GeeksforGeeks.org");
        al.add("www.GeeksforGeeks.org");
        al.add("code.GeeksforGeeks.org");
        al.add("write.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<String> v = new Vector<String>();
        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  www.GeeksforGeeks.org  code.GeeksforGeeks.org  write.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.


Next Article
ArrayList vs LinkedList in Java

N

Nitsdheerendra
Improve
Article Tags :
  • Java
  • Java-ArrayList
  • Java-Collections
  • java-list
  • Java-List-Programs
Practice Tags :
  • Java
  • Java-Collections

Similar Reads

  • ArrayList vs LinkedList in Java
    An array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together. However, the limitation of the array is that the size of the array is predefined and fixed. There are multiple ways to solve this problem. In this article, the diff
    5 min read
  • Convert Vector to ArrayList in Java
    There are multiple ways to convert vector to ArrayList, using passing the Vector in ArrayList constructor and by using simple vector traversal and adding values to ArrayList. Approach 1: Create a Vector.Add some values in Vector.Create a new ArrayList.Traverse vector from the left side to the right
    3 min read
  • ArrayList in Java
    Java ArrayList is a part of the collections framework and it is a class of java.util package. It provides us with dynamic-sized arrays in Java. The main advantage of ArrayList is that, unlike normal arrays, we don't need to mention the size when creating ArrayList. It automatically adjusts its capac
    10 min read
  • Java ArrayList set() Method
    The set() method of the ArrayList class in Java is used to replace an element at a specified position with a new value. This is very useful when we need to update an existing element in an ArrayList while maintaining the list's structure. Example 1: Here, we will use the set() method to update an el
    2 min read
  • Array vs ArrayList in Java
    In Java, an Array is a fixed-sized, homogenous data structure that stores elements of the same type whereas, ArrayList is a dynamic-size, part of the Java Collections Framework and is used for storing objects with built-in methods for manipulation. The main difference between array and ArrayList is:
    5 min read
  • Arrays.stream() Method in Java
    Arrays.stream() method is used to get a sequential Stream from the elements of an array passed as a parameter. Below is a simple example that uses Arrays.stream() to convert a String array into a Stream for sequential processing. Example: [GFGTABS] Java // Java program to demonstrate Arrays.stream()
    3 min read
  • Vector Class in Java
    The Vector class in Java implements a growable array of objects. Vectors were legacy classes, but now it is fully compatible with collections. It comes under java.util package and implement the List interface. Key Features of Vector: It expands as elements are added.Vector class is synchronized in n
    12 min read
  • ArrayList listIterator() Method in Java
    The listIterator() method of ArrayList class in Java is used to return a ListIterator for traversing the elements of the list in proper sequence. The iterator allows both forward and backward traversal of the list. Example 1: Here, we use the listIterator() method to obtain a ListIterator over all e
    3 min read
  • Array of ArrayList in Java
    We often come across 2D arrays where most of the part in the array is empty. Since space is a huge problem, we try different things to reduce the space. One such solution is to use jagged array when we know the length of each row in the array, but the problem arises when we do not specifically know
    2 min read
  • Java ArrayList subList() Method
    The subList() method of the ArrayList class in Java is used to retrieve a portion of an ArrayList between specified indices. When we perform any modifications to the sub list, that will reflect in the original list and vice versa. Example 1: Here, we use the subList() method to extract a sublist fro
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences