HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / Collections Framework / Java ArrayList / How to convert Vector to ArrayList in Java

How to convert Vector to ArrayList in Java

Learn to convert Vector to ArrayList in Java with example. We will also learn to convert arraylist to vector in Java.

1. Convert Vector to ArrayList

To convert a vector containing objects to an arraylist containing similar objects, we can use the arraylist constructor which accepts another collection and initialize the arraylist with the elements of vector.

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

public class ArrayListExample 
{
    public static void main(String[] args) 
    {
        Vector<String> vector = new Vector<>();
        
        vector.add("A");
        vector.add("B");
        vector.add("C");
        vector.add("D");
        
       ArrayList<String> arrayList = new ArrayList<>(vector);
       
       System.out.println(arrayList);
    }
}

Program output.

[A, B, C, D]

2. Convert ArrayList to Vector

The conversion from arraylist to vector is very similar to previous example. Here we have to use the vector constructor. It accepts another collection and initialize the vector with the elements of arraylist.

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

public class ArrayListExample 
{
    public static void main(String[] args) 
    {
        ArrayList<String> arrayList = new ArrayList<>();
        
        arrayList.add("A");
        arrayList.add("B");
        arrayList.add("C");
        arrayList.add("D");
        
       Vector<String> vector = new Vector<>(arrayList);
       
       System.out.println(vector);
    }
}

Program output.

[A, B, C, D]

Happy Learning !!

Read More:

A Guide to Java ArrayList
ArrayList Java Docs
Vector Java Docs

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. John

    November 25, 2019

    Are these examples backwards, or am I reading this wrong? Example 1 is for converting a vector into an arraylist, but the arraylist is made first, then the vector is created to copy the vector. Either way, it’s easy to figure out how it works, thanks to your examples.

    • Lokesh Gupta

      November 25, 2019

      Updated the examples.

Comments are closed on this article!

Search Tutorials

ArrayList Methods

  • ArrayList – Introduction
  • ArrayList – add()
  • ArrayList – addAll()
  • ArrayList – clear()
  • ArrayList – clone()
  • ArrayList – contains()
  • ArrayList – ensureCapacity()
  • ArrayList – forEach()
  • ArrayList – get()
  • Arraylist – indexOf()
  • Arraylist – lastIndexOf()
  • ArrayList – listIterator()
  • ArrayList – remove()
  • ArrayList – removeAll()
  • ArrayList – removeIf()
  • ArrayList – retainAll()
  • ArrayList – sort()
  • ArrayList – spliterator()
  • ArrayList – subList()
  • ArrayList – toArray()

ArrayList Examples

  • ArrayList – Initialize arraylist
  • ArrayList – Iteration
  • ArrayList – Add/replace element
  • ArrayList – Add multiple elements
  • ArrayList – Check empty list
  • ArrayList – Remove element
  • ArrayList – Replace element
  • ArrayList – Empty arraylist
  • ArrayList – Synchronized arraylist
  • ArrayList – Compare two lists
  • ArrayList – Remove duplicates
  • ArrayList – Merge two lists
  • ArrayList – Serialization
  • ArrayList – Swap two elements
  • Convert ArrayList to Array
  • Convert Array to ArrayList
  • Convert HashSet to ArrayList
  • Convert LinkedList to ArrayList
  • Convert Vector to ArrayList
  • ArrayList vs LinkedList
  • ArrayList vs Vector

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces