BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Difference between ArrayList and Vector in Java

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java

ArrayList and Vector both use Array as a data structure internally. However there are key differences between these classes. In this guide, you will learn the differences between ArrayList and Vector.

ArrayList Vs Vector: Differences between them

ArrayList Vector
ArrayList is non-synchronized, which means multiple threads can work on ArrayList at the same time. For example: if one thread is performing an add operation on ArrayList, there can be an another thread performing remove operation on ArrayList at the same time in a multithreaded environment. Vector is synchronized. This means if one thread is working on Vector, no other thread can get a hold of it. Unlike ArrayList, only one thread can perform an operation on vector at a time.
ArrayList can grow and shrink dynamically, it grows by half of its size when resized. Like ArrayList, Vector can grow and shrink dynamically, however it grows by double of its size when resized.
ArrayList gives better performance (fast) for operations such as search, add, delete etc. This is because it is non-synchronized, which means multiple threads can perform different operations on it at the same time. Vector is slow compared to ArrayList. Vector operations gives poor performance as they are thread-safe, the thread which works on Vector gets a lock on it which makes other thread wait till the lock is released.
ArrayList is not a legacy class. Vector is a legacy class.
ArrayList uses iterator to traverse the elements. Vector can use iterator as well as Enumeration to traverse the elements.

Other key differences:
fail-fast: First let me explain what is fail-fast: If the collection (ArrayList, vector etc) gets structurally modified by any means, except the add or remove methods of iterator, after creation of iterator then the iterator will throw ConcurrentModificationException. Structural modification refers to the addition or deletion of elements from the collection.

As per the Vector javadoc, the Enumeration returned by Vector is not fail-fast. On the other side the iterator and listIterator returned by ArrayList are fail-fast.

Legacy?: The vector was not the part of collection framework, it has been included in collections later. It can be considered as Legacy code. There is nothing about Vector which List collection cannot do. Therefore Vector should be avoided. If there is a need of thread-safe operation make ArrayList synchronized as discussed in the next section of this post or use CopyOnWriteArrayList which is a thread-safe variant of ArrayList.

There are few similarities between these classes which are as follows:

  1. Both Vector and ArrayList use growable array data structure.
  2. The iterator and listIterator returned by these classes (Vector and ArrayList) are fail-fast.
  3. They both are ordered collection classes as they maintain the elements insertion order.
  4. Vector & ArrayList both allows duplicate and null values.
  5. They both grows and shrinks automatically when overflow and deletion happens.

When to use ArrayList and when to use vector?

It totally depends on the requirement. If there is a need to perform “thread-safe” operation the vector is your best bet as it ensures that only one thread access the collection at a time.

Update: Even if you need to perform synchronized operations, you can still use ArrayList by converting it to a Synchronized ArrayList.

Performance: Synchronized operations consumes more time compared to non-synchronized ones so if there is no need for thread safe operation, ArrayList is a better choice as performance will be improved because of the concurrent processes.

How to make ArrayList synchronized?
As I stated above ArrayList methods are non-synchronized but still if there is a need you can make them synchronized like this:

//Use Collecions.synzhonizedList method
List list = Collections.synchronizedList(new ArrayList());
...

//If you wanna use iterator on the synchronized list, use it
//like this. It should be in synchronized block.
synchronized (list) {
  Iterator iterator = list.iterator();
  while (iterator.hasNext())
      ...
      iterator.next();
      ...
}

Example of ArrayList in Java

In this example, we have an ArrayList that contains fruit names. We are iterating the arraylist using iterator.

import java.util.*;
class JavaExample{
  public static void main(String args[]){

    List<String> fruits=new ArrayList<String>();
    fruits.add("Apple");
    fruits.add("Mango");
    fruits.add("Orange");
    fruits.add("Banana");
    //Iterating the array list using iterator
    System.out.println("ArrayList elements: ");
    Iterator itr=fruits.iterator();
    while(itr.hasNext()){
      System.out.println(itr.next());
    }
  }
}

Output:
Difference between ArrayList and Vector

Example of Vector in Java

In this example, we have created a vector and added few elements to it. We are iterating this vector using enumeration.

import java.util.*;
class JavaExample{
  public static void main(String args[]){
    Vector<String> names=new Vector<String>();
    names.addElement("Chaitanya");
    names.addElement("Ajeet");
    names.addElement("Hari");

    System.out.println("Vector elements: ");
    //Iterating vector using Enumeration
    Enumeration e=names.elements();
    while(e.hasMoreElements()){
      System.out.println(e.nextElement());
    }
  }
}

Output:
Difference between ArrayList and Vector

Recommended articles:

  • ArrayList vs LinkedList
  • ArrayList vs HashMap
  • Convert ArrayList to Array
❮ Java ArrayListJava Collections ❯

Top Related Articles:

  1. OOPs in Java: Encapsulation, Inheritance, Polymorphism, Abstraction
  2. How to sort Hashtable in java
  3. Difference between HashMap and Hashtable
  4. Constructor Overloading in Java with examples
  5. Java Scanner class with examples

Tags: Collections, Java-ArrayList, Java-Vector

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. Akash says

    January 28, 2014 at 12:24 PM

    1) Synchronization: ArrayList is non-synchronized

    3) Performance: ArrayList gives better performance as it is synchronized.

    Which one to believe?

    Reply
    • Chaitanya Singh says

      February 11, 2014 at 2:47 PM

      Hi Akash,
      Thanks for bringing that to my attention. It was a typo. I fixed it :)

      Regards,
      Chaitanya

      Reply
  2. Akash says

    February 12, 2014 at 7:00 PM

    can i get all these files in pdf? not so comfortable in reading online.

    Reply
  3. sandip soliya says

    September 12, 2014 at 6:17 AM

    great job,thanks for making this very nice site…

    Reply
  4. Majid Arif Khakwani says

    September 24, 2014 at 5:59 AM

    Thanks for info good comparison!!!!!!!!!

    Reply
  5. CB Singh says

    November 27, 2014 at 4:13 AM

    The iterator and listIterator returned by these classes (Vector and ArrayList) are fail-fast.
    I think above line is not correct in similarity section-
    Because Vector is not fail fast and other thing Vector is using Enumerator not Iterator. Please correct if I am wrong.

    Reply
  6. santhu says

    December 22, 2015 at 10:38 PM

    can u please explain about ConcurrentModificationException in detail…

    Reply
  7. Rishikesh Agrawani says

    January 1, 2016 at 5:37 PM

    Your site is very useful and one of the best java’s knowledge provider site for beginners and advance programmers. The way you provide a brief and sufficient description about any topic is very good as examples are already included with almost each and everyone.I am a fan of your site.Its look is also good and the fonts used to write the topic’s description and program’s of java is too good.You have used the hyperlink among the description to provide special help about that topics, it’s good.I am reading java’s collection framework now and trying to take the benefit of your site.Very very thank you.I am very thankful to you…

    Reply
  8. Shakthi says

    April 13, 2016 at 12:56 PM

    You have really made an excellent job thank you Chaitanya Singh ..Collections was really an nightmare but its no more.You really don’t know what a great help you have done to me.Thanks again!!!!!

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap