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:
- Conversion of Arrays to List
- Conversion of Arrays to List in Java 8
- Conversion of ArrayList to Arrays in Java – 2 ways
- Conversion of Arrays to Vector
- Conversion of Arrays to HashSet
- Conversion of HashSet to Arrays
- Conversion of LinkedList into Vector
- Conversion of ArrayList into HashSet to remove duplicate elements
- How to convert LinkedList to contain unique elements in ascending order
- Converting Ordered ArrayList into Sorted TreeSet
- Conversion of Map to List in Java
- Conversion of Map to List in Java 8
- Conversion of List to Map in Java
- Conversion of List to Map in Java 8
- How to remove elements while iterating collection object
References:
- https://docs.oracle.com/javase/tutorial/collections/intro/
- https://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
- https://docs.oracle.com/javase/tutorial/collections/interfaces/list.html
- https://docs.oracle.com/javase/tutorial/collections/implementations/list.html
- https://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Vector.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Vector.html
Happy Coding !!
Happy Learning !!