Rule Definition
Since Vectors are all synchronized - yet synchronization is usually not needed -, they are much slower than the various List and Map implementations
Remediation
When synchronization is not need, prefer List and Map implementations
Violation Code Sample
import java.util.*; public class VectorDemo { public static void main(String args[]) { Vector<String> vec_tor = new Vector<String>(); //Violation // Use add() method to add elements in the Vector vec_tor.add("Geeks"); vec_tor.add("for"); vec_tor.add("Geeks"); vec_tor.add("10"); vec_tor.add("20"); // Displaying the Vector System.out.println("Vector: " + vec_tor); // Fetching the specific element from the Vector System.out.println("The element is: " + vec_tor.get(2)); } } ------------------------------------------------ .... Type type = field.getGenericType(); if (type instanceof ParameterizedType) { ParameterizedType ptype = (ParameterizedType) type; if(c.getSimpleName().equals("Vector")){ Class pta = (Class) ptype.getActualTypeArguments()[0]; Vector<what to put here> v = (Vector)field.get(obj); if(v == null){ v = new Vector<what to put here>(); field.set(obj, v); } ....
Fixed Code Sample
import java.util.*; public class ArrayListDemo { public static void main(String args[]) { // Creating an empty Vector ArrayList<Integer> arr_list = new ArrayList<Integer>(n); // Use add() method to add elements in the Vector arr_list.add("10"); arr_list.add("20"); arr_list.add("30"); arr_list.add("40"); arr_list.add("50"); // Displaying the Vector System.out.println("ArrayList: " + arr_list); // Fetching the specific element from the Vector System.out.println("The element is: " + arr_list.get(2)); } }
Reference
https://www.techiedelight.com/why-vector-class-java-obsolete/
Related Technologies
JEE
Technical Criterion
Efficiency - Memory, Network and Disk Space Management
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.