Difference between ArrayList and Vector in java

One of the common interview question is “What is difference between ArrayList and Vector”.Before we actually see differences,let me give you brief introduction of both.

ArrayList

  • ArrayList is implementation of list interface.
  • ArrayList is not synchonized(so not thread safe)
  • ArrayList is implemented using array as internal data structure.It can be dynamically resized .
  • ArrayList increases half of its size when its size is increased.

Vector

  • Vector is implementation of list interface.
  • Vector is synchonized(so thread safe)
  • Vector is implemented using array as internal data structure.It can be dynamically resized.
  • Vector doubles size of array when its size is increased.

ArrayList vs Vector:

Parameter
Vector
ArrayList
Synchonized
Yes
No
ThreadSafe
Yes
No
Performance
It is slower than arraylist
It is faster than Vector
Changes in internal  size of array when  resized
Vector doubles size of its internal  array when its size is increased
ArrayList increases half of its size when its size is increased.

Which is better? ArrayList or Vector?

It actually depends on our need.Vector is slower than ArrayList as its methods are synchronized so if we don’t work in multi threaded environment then ArrayList is better choice.

💻 Awesome Tech Resources:
  • Looking for ⚒️ tech jobs? Go to our job portal.
  • Looking for tech events? Go to tech events 🗓️ Calendar.️

Best Practice:

When we initialize ArrayList or Vector,always initialize with largest capacity java program will need as incrementing size is costlier operation.

Can we synchronize ArrayList?

Yes,ArrayList can also be synchonized with help of method Collections.synchronizedList(arraylist)


import_contacts

You may also like:

Related Posts

  • 15 April

    How to Deep Copy Arraylist in Java

    Learn about how to create deep copy of ArrayList in different ways.

  • 02 October

    Initialize ArrayList with values in Java

    In this article, we will learn to initialize ArrayList with values in Java. ArrayList is an implementation class of List interface in Java. It is used to store elements. It is based on a dynamic array concept that grows accordingly. We can Initialize ArrayList with values in several ways. Let’s see some of them with […]

  • 17 March

    PriorityQueue in Java 8

    In this post, we will see about Java 8 PriorityQueue. When the objects are supposed to be processed on the basis of their priority, in that scenario we use PriorityQueue. It’s a special type of queue (also, unbound queues) where the elements can be ordered either as per their natural ordering or based on a […]

  • 06 October

    2d Arraylist java example

    In this post, we will see how to create 2d Arraylist in java. Best way to create 2d Arraylist is to create list of list in java. [crayon-607a622bda097916739326/] Let’s create a program to implement 2d Arraylist java. 💻 Awesome Tech Resources: Looking for ⚒️ tech jobs? Go to our job portal. Looking for tech events? […]

  • 04 May

    How HashMap works in java

    Most common interview questions are <code>How HashMap works in java</code>, “How get and put method of HashMap work internally”. Here I am trying to explain internal functionality with an easy example. [crayon-607a622bda565991310824-i/]  is one of the most used Collections in java.Rather than going through theory, we will start with example first, so that you will […]

  • 21 October

    How to Sort HashSet in Java

    In this post, we will see how to sort HashSet in java. HashSet is a collection which does not store elements in any order. You might come across a situation where you need to sort HashSet. There can be many ways to sort HashSet, we will see two methods here. 💻 Awesome Tech Resources: Looking […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.