C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Diamonds: The right side of the Vector declaration uses diamond inference syntax. The "Integer" type is inferred by the compiler.
Java program that uses Vector
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);
}
}
}
Output
100
1000
Java program that uses get
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);
}
}
}
Output
20
30
40
Java program that uses set, setElementAt
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));
}
}
Output
20
30
40
Size: Finally we use the size() method on the Vector. Three elements (Strings) are stored within the collection.
StringsJava program that adds ArrayList to Vector
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());
}
}
Output
3
Java program that converts Vector, array
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);
}
}
}
Output
one
two
three
Java program that uses contains, indexOf
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);
}
}
Output
Contains 15
index of 20 = 3
Version 1: This version of the code adds Integers to an ArrayList in a tight loop.
Version 2: Here we do the same thing as in version 1, but use a Vector instead. We call add() on the Vector instance.
Result: The ArrayList, which is not thread-safe, is about twice as fast as Vector when adding many elements.
Thus: Replacing Vector with ArrayList will likely lead to a performance increase in programs.
Java program that benchmarks ArrayList, Vector
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);
}
}
Output
5 ms, ArrayList
10 ms, Vector