Difference ArrayList Vector

Difference ArrayList Vector

Answer: Below are differences:

ArrayList Vector
ArrayList is not synchronized (Not thread safe) Vector is synchronized (Thread safe)
ArrayList does not contains legacy methods Vector class contains several legacy methods those are not part of Collections
Data growth: If internal array out of space then its increases size by 50 percent Data growth: If internal array out of space then its size increases double the size of array

Java example:

package com.javahonk.arraylistvector;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

public class ArrayListVector {

    public static void main(String[] args) {
    
    Vector<String> vector = new Vector<String>();
    vector.add("Java");
    vector.add("Honk");
    vector.add("Test");
    vector.add(null);
    vector.add(null);
    
    System.out.println("Vector example\n");
    for (String string : vector) {
        System.out.println(string);
    }
    
    System.out.println("\nArrayList example\n");
    List<String> list = new ArrayList<String>();
    list.add("Java");
    list.add("Honk");
    list.add("Test");
    list.add(null);
    list.add(null);
    
    for (String string : list) {
        System.out.println(string);
    }
    
    

    }

}

Output:

Difference ArrayList Vector

That’s it Difference ArrayList Vector

Leave a Reply

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