Vector
This Java collection is similar to the ArrayList
. It will resize to accommodate more elements. We call add()
to place elements at the end.
Vector
, which is part of java.util.Vector
, is not the most optimized list collection in Java. It is fine for older code, but new code should use ArrayList
.
for
-loopThis program shows the basic syntax for Vector
. It adds 2 Integers to an Integer Vector
. It then uses the for
-loop to display those elements to the console.
Vector
declaration uses diamond inference syntax. The "Integer" type is inferred by the compiler.import java.util.Vector; public class Program { public static void main(String[] args) { // Add two values to Vector. Vector<Integer> v = new Vector<>(); v.add(100); v.add(1000); // Display values. for (int value : v) { System.out.println(value); } } }100 1000
We access elements in a Vector
with the get()
method. And with a for
-loop, we can call get()
on all indexes. This accesses every element in the Vector
.
import java.util.Vector; public class Program { public static void main(String[] args) { Vector<Integer> v = new Vector<>(); v.add(20); v.add(30); v.add(40); // ... Get element 0. System.out.println(v.get(0)); // ... Loop over all indexes, calling get. for (int i = 0; i < v.size(); i++) { int value = v.get(i); System.out.println(value); } } }20 30 40
Set()
assigns an element value at an index within the vector. It returns the value of the old, replaced element. SetElementAt
is an older method—its arguments are reversed.
import java.util.Vector; public class Program { public static void main(String[] args) { // Create one-element Vector. Vector<Integer> v = new Vector<>(); v.add(20); // Set index 0. int old = v.set(0, 30); // Display old and current value. System.out.println(old); System.out.println(v.get(0)); // Set index 0 with setElementAt. v.setElementAt(40, 0); System.out.println(v.get(0)); } }20 30 40
AddAll
, ArrayList
Here we add the contents of an ArrayList
to a Vector
. We add three elements to ArrayList
. Then we call addAll
to add them to our Vector
.
size()
method on the Vector
. Three elements (Strings) are stored within the collection.import java.util.ArrayList; import java.util.Vector; public class Program { public static void main(String[] args) { // Create new ArrayList. ArrayList<String> values = new ArrayList<>(); values.add("cat"); values.add("dog"); values.add("mouse"); // Add ArrayList to Vector. Vector<String> v = new Vector<String>(); v.addAll(values); // Display size of Vector. System.out.println(v.size()); } }3
Vector
to arraySuppose we have a Vector
, but want an array. With the toArray
method, this conversion is easily done. The empty String
array is allocated and filled.
import java.util.Vector; public class Program { public static void main(String[] args) { // Add ArrayList to Vector. Vector<String> v = new Vector<>(); v.add("one"); v.add("two"); v.add("three"); // ... Convert Vector into String array. String[] array = {}; String[] result = v.toArray(array); for (String value : result) { System.out.println(value); } } }one two three
With contains and indexOf
we search a Vector
. Contains
returns true if the specified element exists. IndexOf
meanwhile returns the element's index (or negative one if not found).
import java.util.Vector; public class Program { public static void main(String[] args) { Vector<Integer> v = new Vector<>(); v.add(5); v.add(10); v.add(15); v.add(20); // See if 15 is present. if (v.contains(15)) { System.out.println("Contains 15"); } // Get index of value 20. int index = v.indexOf(20); System.out.println("index of 20 = " + index); } }Contains 15 index of 20 = 3
Consider the Vector
: the most similar collection in Java is the ArrayList
. Here we benchmark an ArrayList
against a Vector
.
ArrayList
in a tight loop.Vector
instead. We call add()
on the Vector
instance.ArrayList
, which is not thread-safe, is about twice as fast as Vector
when adding many elements.Vector
with ArrayList
will likely lead to a performance increase in programs.import java.util.ArrayList; import java.util.Vector; public class Program { public static void main(String[] args) { long t1 = System.currentTimeMillis(); // Version 1: add to ArrayList. ArrayList<Integer> a = new ArrayList<>(); for (int i = 0; i < 100000; i++) { a.add(i); } long t2 = System.currentTimeMillis(); // Version 2: add to Vector. Vector<Integer> v = new Vector<>(); for (int i = 0; i < 100000; i++) { v.add(i); } long t3 = System.currentTimeMillis(); // ... Times. System.out.println(t2 - t1); System.out.println(t3 - t2); } } 5 ms, ArrayList 10 ms, Vector
Sort
We sort a Vector
by passing it to the Collections.sort
method, found in java.util.Collections
. The Vector
is sorted in-place. By default the sorting order is ascending.
Stack
In the Stack
collection, a LIFO (last-in-first-out) algorithm is used. Stack
is based on Vector
: for this reason it is deprecated.
In programs with threads, a Vector
may be a better choice than ArrayList
. And Vector
is used in older programs that precede ArrayList
. Removing Vector
is often an optimization.