Conversion of LinkedList into Vector

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

 

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);

 

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

 

References:

 

Happy Coding !!
Happy Learning !!

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