Java – Conversion of LinkedList into Vector

In this article, we will discuss how to convert LinkedList object into a synchronized Vector object

1. Vector v/s LinkedList :

  • Both classes implements List interface and maintains insertion-order
  • Vector is introduced in Java 1.0 version, whereas LinkedList introduced in Java 1.2 version
  • Vector is legacy class and all its method are synchronized
  • LinkedList is not synchronized
  • But we can make it synchronized using Collections class’ static utility method Collections.synchronizedList(lst);

2. Conversion of LinkedList to Vector :

  • Use inter-conversion collection constructor

ConvertLinkedListToVector.java

package in.bench.resources.java.collections;

import java.util.Enumeration;
import java.util.LinkedList;
import java.util.Vector;

public class ConvertLinkedListToVector {

	public static void main(String[] args) {

		// creating ArrayList object of type String
		LinkedList<String> techCompanies = new LinkedList<String>();

		// adding elements to LinkedList object
		techCompanies.add("Sun");
		techCompanies.add("Apple");
		techCompanies.add("Whatsup");
		techCompanies.add("BEA Weblogic");
		techCompanies.add("JBoss");

		// convert to Vector using inter-conversion constructor
		Vector<String> v = new Vector<String>(techCompanies);

		System.out.println("Enumerating Vector elements\n");

		// enumerating Vector elements
		Enumeration<String> e = v.elements();

		while(e.hasMoreElements()){
			System.out.println(e.nextElement());
		}
	}
}

Output:

Enumerating Vector elements

Sun
Apple
Whatsup
BEA Weblogic
JBoss

Note: both LinkedList and Vector classes maintains insertion-order, as seen from above example

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java - Conversion of Arrays to Vector
Java - How to remove duplicate elements from ArrayList maintaining insertion-order ?